- Home /
How do you call child in group of elements?
I have a game object called objects that contains 4 game objects. In code I am object pooling and moving all 4 objects at once and recycling them. objects[currentObject] is how new pairs are created and then recycled. If the 4 objects in a single game object are named Object1, Object2, Object3, Object4 how do I call one of those objects out of object so I may setActive(false)?
Answer by Reynarz · Jul 19, 2017 at 09:38 PM
If i understood you well, are you working with Child-parent relationship? If the answer is yes. in your code you can do:
transform.GetChild(/*Number of the child*/);
Answer by Brogan89 · Jul 19, 2017 at 09:57 PM
If you are looking for a script in child gameObjects you could use GetComponentsInChildren<T>()
which will return an array of type T
, T
being your script. Then loop through that array and SetActive(false)
.
Otherwise if its not script or component sepecific and you just want the gameObjects use a for loop
for (int i = 0; i < transform.childCount; i++) transform.GetChild(i).gameObject.SetActive(false);
or if you are looking for a specific object like a gameObject named "Object4" use transform.Find("Object4")