- Home /
How does gameobject.find work?
Hello, all. I'm fairly new to coding, and Unity in general, and I need a little help.
I'm trying to set up an instanced jump pad prefab that I can plop down into my levels anywhere, facing any direction, and not have to edit them. I'd like for them to function similar to those of 3D sonic games, where when you hit them it always launches you in the pad's forward direction.
The way I was going to set them up is by having an empty game object child that sits just in front of the pad, facing the pad's forward direction. On collision, I was going to have the jump pad's script check what object has collided with it, then check if it has a rigid body component. If it does, I'd like it to set that object's position and rotation equal to the empty pad direction object, then apply force to the colliding object's forward direction.
The problem is that gameobject.find() seems to search the ENTIRE SCENE hierarchy, starting from the top of the list as seen in the editor and working it's way down each object and it's children in top to bottom order. I need to figure out how to get the function to only search within the object running the script, and it's own children, for this padDirectionObject, skipping over anything that's not inside the instance running the script.
How can I do this?
I don't think you can use GetComponent to get an object, can you? Are child objects considered components?
GetComponentInChildren and/or GetChild are what you are looking for.
I figured out a different way to achieve the same behavior.
Rather than using gameObject.find("padDirectionObj"); I simply made a public GameObject padDirectionObj variable, then dropped the child directionObj into it in the Unity inspector.
Now each instance knows to grab it's child direction object for the script! :D
For the sake of other's possibly looking for the same information though, I haven't marked this as the answer. It solves my specific intent for the moment, but I'd still like to know if there's a way to define the scope of the gameobject.find function.
It's a feature I never used, except maybe for some super quick prototyping. It's slow, potentially introducing errors (of finding something else than expected) and there's always a better way how to achieve the same thing
Answer by HenriMR · Nov 18, 2019 at 09:26 PM
Yes, Find() searches through all game objects. The way I would do to restrict the seach to the game objects with a script is to put a static list of the type of the script and on the Awake() or Start() add the script to the list. Then you can search only on this list.
private List<MyScript> list;
private Awake()
{
list.Add(this);
}
void Search()
{
foreach(MyScript script in list)
{
script.gameObject.transform.Find("name of child");
}
}
Hope its helpfull.
Links:
1- https://docs.unity3d.com/ScriptReference/GameObject.Find.html
2- https://docs.unity3d.com/ScriptReference/Transform.Find.html
Forgot one thing:
void OnDestroy
{
list.Remove(this);
}
It´s important, or it can cause errors if the gameObject is destroyed and not removed.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Player movement ignores some collision 0 Answers
Problems with 2D Collision 3 Answers
How to call OnTriggerEnter once 5 Answers