- Home /
Access the a child of a GameObject through a script that is assigned to it
I have a problem in making a little game in Unity 5. I have a prefab (named "Controller") and it is instantiated two times in the game. This prefab has a script attached to it, and also has 4 children of type GameObject. How can i access a child to the clone that it is instantiated? I need to change it's layer to Ignore Raycast if a button is pressed, but i don't know how to do it.
Answer by TeohRIK · May 01, 2015 at 03:53 AM
maybe you can use GetComponentsInChild or GetComponentInChild to access the script
http://docs.unity3d.com/ScriptReference/Component.GetComponentsInChildren.html http://docs.unity3d.com/ScriptReference/Component.GetComponentInChildren.html
Answer by Kiwasi · May 01, 2015 at 04:03 AM
A Transform can be iterated across to get the transforms of all of its children. There are also a variety of methods to find children, such as Transform.Find.