Unable to exlude parent gameObjects and identify them - c#

My aim is for gameObjects to shoot other gameObjects under the same tag in their proximity, excluding the object who has fired. Though, I have issues concerning bullets being unable to register their parent as a non-enemy. As my current method involves detecting gameObjects of a tag.
For those who are still unclear, in my code I have written in a bullet script:
if (gameObject.transform.tag is not PARENT)
{
target_object = GameObject.FindGameObjectWithTag("Sudo Code-->All but parent Entity");
}"
In an attempt to locate all gameObjects with the same tag including the parent. It does its job locating all gameObjects of a tag, but I can't find a way to exclude the object who fired the bullet in the first place.
What method best fits this situation? I've come up with a few ideas but all feels over-complicated and unreasonable. Since all is needed is to identify the parent and not target it.
Note: I have over-described many areas and put main ideas in bold due to different users being short-tempered idiots. I know most of you aren't them :)
Just for those few, if posing a question please be patient allowing for an answer to be submitted first, before taking rude actions. Thanks!

Related

How do I get text UI to appear when a condition in another script is met?

this is my first question so I don't know much about 'question etiquette' as it were. I'm working in C# in Unity, with Visual Studio.
I'm a little stuck trying to get text to appear when another script has a condition met.
A script (let's call it ManagerScript) attached to an empty (let's call it Manager) that manages some enemies that get destroyed counts the amount of enemies that are destroyed, and once that number hits 8, I want text to appear that tells the user they win.
How can I do this? I have some text UI (let's call the object WinnerText) on a Canvas Panel, but I don't know how to make it 'active' only once the player wins.
Please don't just give me the answer - please explain the reasoning as well if you can, so I can do the same thing for when the player loses the game.
Thanks in advance!
Golden Viper
Your ManagerScript that keeps track of counting should check for your winning/losing condition after it was modified, like:
number++;
if(number > x)
{
// Display Text
}
To display your text or other ui element. You need some kind of reference to the gameobject or the component directly. Since the ManagerScript does not know about it, you need to give it a reference to the Component/GameObject you want to activate/deactivate.
Unity offers multiple ways of getting your reference. For example you can declare within your ManagerScript a public GameObject MyUiElement (instead of gameobject you can also just have the component you need f.e. 'Text'). If you declared a public Property, a new area within the inspector of your 'Manager' will appear where you can reference your Object by drag & drop or simply by clicking and sleecting.
Other ways of getting the desired gameobjects reference would be with GameObject.Find or similar. For more information about find see: https://docs.unity3d.com/ScriptReference/GameObject.Find.html
If you got your GameObjects reference you can manipulate it like
number++;
if(number > x)
{
MyUiElement.SetActive(true/false);
}

Click to move should i verify by tag or by game object?

Recently i came across developing a RPG game as diablo and basically the movement control needs to be carefuly made and verify every element we click before move. I was wondering by having alot of prefab of enemy gameobject, the UI and myself if i must verify by the elements with the tag i want or by the gameobject.
Well for example if i have all UI gameobjects with tag UI and enemys with tag enemy, is it good to verify by tag instead of verying directly by the gameobject name? I'm quite confused with i should use.
This depends on how many GameObjects you are verifying and how often you do it. If you don't this every frame or very often then don't worry about it. If you do it every frame or very often then check the GameObject by tag instead of by name but make sure to use the GameObject.CompareTag function to do so not with the GameObject.tag variable.
The reason is because GameObject.name and GameObject.tag allocates memory. GameObject.CompareTag does not.

Unity 2D - Dynamically instantiate new prefab inside NGUI's "UI Root"(as Parent)

Im new to unity. When I instantiate a new prefab GameObject from inside script as follows:
GameObject newArrow = (GameObject)Instantiate(arrowPrefab);
newArrow.transform.position = arrowSpawnTransform.position;
But this is creating the object in the root hierarchy(and not inside "UI Root" of NGUI). When I add any object outside of the UI-Root (of NGUI) it adds it to some location far away from the camera, also with a huge dimension. Can someone help me with how to add the newly created prefab under "UI Root" ?
It would be great of someone also lets me know about the positioning and scaling associated with native unity and NGUI. I try hard but am not understanding where to keep what object and in what size so that it comes out as expected. I'll appreciate any help that can be provided. Thanks !
EDIT:
I have found a way to place the new prefab inside "UI Root" thru:
newArrow.transform.parent = gameObject.transform.parent;
after instantiating.
But still the scaling is huge. It's like multiple times bigger than the screen size. Please help me with this. What should I be doing ?
When working with UI elements in NGUI, don't use Instantiate. Use NGUITools.AddChild(parent, prefab).
NGUI's scale with respect to the rest of the objects in the scene is rather microscopic. If you look at the UIRoot object, you will notice that its scale is measured in thousandths of a meter (default object resolution of 1 typically represents a meter). That is why when you instantiate a prefab and attach it to any object under the UIRoot it appears gigantic relative to the scale of the UIPanel. You could manually scale down the object to the appropriate size, or let NGUI do it for you (as indicated by Dover8) by utilizing NGUITools.AddChild(parent, prefab). In fact, this is the proper way to do so. Don't try to do it manually. The results will be unpredictable and can lead to inappropriate behavior of components. Here's a link to Tasharen's forum on this topic: http://www.tasharen.com/forum/index.php?topic=779.0
Positioning is a bit more complicated. Everything is relative to anchors. Anchor positions are a relationship between a parent object (usually a panel) and a target (usually some form of widget such as a sprite or label). They are controlled by four values relative to the edges of the panel (or parent object), right, left, bottom, and top with respect to the edges of the target (varies by position). Each of these are modified by an integer value (in pixels) that modify the dimensions of the target relative to the parent. There are many examples included with NGUI. I suggest that you look over them. In particular, pay attention to Example 1 - Anchors. I learned a lot from studying these examples, but they don't really cover the full power of NGUI, but they are an excellent starting point.

Hide and seek game and raycasting

I am working on multiplayer collaboration in Unity3d using Smartfox Server2x.
But I wish to change it in to a hide and seek game.
When the hider (third person controller) presses the button "seek me" the seeker tries to find the hider. But I don't know How can I identify when a seeker sees the hider. Is it possible using Raycasting. If yes how? Please help me.
void Update () {
RaycastHit hit;
if(Physics.Raycast(transform.position,transform.forward,out hit,50))
{
if(hit.collider.gameObject.name=="Deccan Peninsula1")
{
Debug.Log("detect.................");
}
if(hit.collider.gameObject.tag =="Tree")
{
Debug.Log("detect.........cube........");
//Destroy(gameObject);
}
}
From Unity Answers by duck:
There's a whole slew of ways to achieve this, depending on the precise
functionality you need.
You can get events when an object is visible within a certain camera,
and when it enters or leaves, using these functions:
OnWillRenderObject, Renderer.isVisible,
Renderer.OnBecameVisible, and OnBecameInvisible
Or you can calculate whether an object's bounding box falls within the
camera's view frustum, using these two functions:
GeometryUtility.CalculateFrustumPlanes
GeometryUtility.TestPlanesAABB
If you've already determined that the object is within the camera's
view frustum, you could cast a ray from the camera to the object in
question, to see whether there are any objects blocking its view.
Physics.Raycast
You could do many things to find out if a seeker has found the hider. I am going to suggest how I would do it and I will try to make the idea/code as efficient as possible.
Each GameObject knows its position via the transform component. You can check how close one object is from the other by creating a ratio and comparing how close they are from each other. The moment both objects are close to each other then you enter a new state. In this state you will fire a RayCast only when the direction/angle of view of the seeker changes. So think of it this way, your seeker is looking around and as he is spinning he is firing the RayCast. The main idea is not to fire way too many RayCasts all the time which will hinder performance. Make sure your RayCast ignores everything except who you are looking for.
If you get stuck you can ask a more specific question, probably regarding RayCast and how to make them ignore walls or not shoot through walls, or maybe you discover that solution on your own.
Good luck!

Matching objects in a drag and drop game

I am developing an educational game where I give the user a selection of words. The objective is to take these words and arrange them into a sentence by dragging each word individually and placing it into order on the provided area. I have an plane ready for each word to be dragged onto, but I am unsure how to determine whether the user has dragged their word into the correct position within the sentence.
I thought maybe I would be able to give an ID/Tag to each word, and a relating ID/Tag to each plane. When the two collide I could compare the two IDs and if they match, consider it a successful placement.
I'm wondering if anyone else has a better solution, as I am not sure about the best way to go about this?
Your idea is good, I don't see why not. Depending on how you implement this you can have much work if you have many words, right?
I suggest you this: make a prefab that consists of 2 colliders (and their respective GameObjects) and one script. One collider for the word and one colider of the correct position. The script would read from somewhere (a resource xml, field in the editor, etc.) and apply the initial position for the word and the position for the 'correct position' collider. This script would also read other information (like the actual word) and make all configuration of these objets.
This way you could easily drag 'word prefabs' to your scene and configure them individually.
Additionally, instead of dragging this prefab, you could have an external script in your scene that would represend that 'level' (if that concept applies to your game...). The idea here is that this script could load the prefabs for that 'level' during runtime. It could even pass all the data for the prefab script to configure the objects, like I said before.
I forgot to say the most important difference: in this method you don't have to worry about ID's. They belong to the same GameObject parent, so you can easily retrieve the objects you want in the script.

Categories

Resources