- Home /
GetComponentInChildren, is there an Alternative way?
I am looking for a way to find a script, that is positioned under the same GameObject, but not in the same hierarchy. Since there is more GameObjects with the same name and the same Script, "find" is not a possibility. I have been looking through the documentation for a sollution, but haven't been able to find anything..My apologies if there is something after all. My glasses might need cleaning ;)
Answer by qJake · Aug 03, 2010 at 05:24 PM
Uh... are you looking for Object.FindObjectOfType? This returns the first instance of whatever component you give it, but be warned, it is slower than GetComponent and friends!
Edit: If you want to find a child component in a different game object, you first need to get its reference. See this post for how to access other game objects and obtain their references:
http://unity3d.com/support/documentation/ScriptReference/index.Accessing_Other_Game_Objects.html
After you have a "GameObject
" object, you can just call myObject.GetComponentInChildren()
to get the component reference in that particular game object.
If I understand FindObjectsOfType Correctly, this will just find a object in the Scene, and not only under the gameobject that I specify?
Oh, you want it under another object? Hang on I'll edit my post, since your question was unclear.
Thank you :) Its a way of solving my problem, but not excatly ;) I'm pretty sure by now that there is no easy way for me to solve this. Let me try to explain a bit better. I have my main GameObject, under this I have two sets of Child hiracies. I want a script in one child hieracy to access a script in the other child hieracy.
Right now I use a script in the main GameObject to access both child scripts, and then pass information through that script.. but I really wanted to access the scripts directly. I am an artist, and started sparetime coding a few months ago, so Im pretty green still :)
Your answer
![](https://koobas.hobune.stream/wayback/20220613070345im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to get Component from Game Object in C# 1 Answer
What's the most efficient way to change script from child object 2 Answers
transform.root and empty root game objects don't play nice 1 Answer
how do I change the properties of a material that are on the children of an object on script? 1 Answer
GetComponent in parent question 3 Answers