- Home /
Iterate through the list of child objects within a game objects
Hi, I have a game object that has many child objects. I want to iterate through the child objects and store in an array of Gameobject[].
Dupe of http://answers.unity3d.com/questions/328866/search-in-child-of-game-object-and-insert-theme-in.html, which has good answer.
Answer by ravi_gohil999 · Jan 25, 2013 at 05:12 AM
Thanks Owen for the reply but I found the solution
This is what I do..... //obj is the game object that I have assigned through the inspector...it has many children //inside it //YOu can simply copy and paste this function and use it...... //The gameobject whose children you want to traverse..
void replaceLightMap (GameObject obj) { for (int i = 0; i < obj.transform.childCount; i++) { Transform tObjects = obj.transform.GetChild (i); GameObject oObject = tObjects.gameObject; print ("oObject is " + oObject);
}
}
This will print the name of all the child objects within the object obj.
So you no longer want them in an array? The 1st Search result (a in comment) shows how to do that, as well,
Your answer
Follow this Question
Related Questions
Creating an array of immediate children of a game object -1 Answers
Find Child of GameObject from Array in Different Script 0 Answers
Use an objects (from array) position to focus a camera on 3 Answers
Cannot convert 'UnityEngine.Collider[]' to 'UnityEngine.gameObject[]' using OverlapSphere 3 Answers
Detection if a GameObject is below you or next to you? 1 Answer