- Home /
The question is answered, right answer was accepted
Recursively get all children
hi, there is a method to get all children of a game object recursively ? Cause I want sirect children but children of children too !?
// work fine for me (geted ALL obj in ierarchy), recurs
// recursively get childrens =======================================================================
private List<GameObject>childsOfGameobject = new List<GameObject>();
private List<GameObject> GetAllChilds(GameObject transformForSearch)
{
List<GameObject> getedChilds = new List<GameObject>();
foreach (Transform trans in transformForSearch.transform)
{
//Debug.Log (trans.name);
GetAllChilds ( trans.gameObject );
childsOfGameobject.Add ( trans.gameObject );
}
return getedChilds;
}
// END recursively get childrens ====================================================================
// sample
void getClothObjs() { GameObject cloth = GameObject.Find ( "girlForGUZoneCLOTH" ); childsOfGameobject.Clear (); GetAllChilds (cloth);
// debug;
foreach (GameObject list in childsOfGameobject)
{
Debug.Log (list.name);
}
}
Here is the code to iterate through all the childs and nested childs of a gameObject.
public List childs = new List();
void Start()
{
FindEveryChild(gameObject.transform);
for (int i = 0; i < childs.Count; i++)
{
FindEveryChild(childs[i]);
}
}
public void FindEveryChild(Transform parent)
{
int count = parent.childCount;
for (int i = 0; i < count; i++)
{
childs.Add(parent.GetChild(i));
}
}
Like this? It calls setlayer recursively.
void Start(){
SetLayer(transform.root, 1);
}
public static void SetLayer (Transform trans, int layer){
//Set the layer Of the parent
trans.gameObject.layer = layer;
// Call set layer on any children
for (int i = 0; i < trans.childCount; i++) {
SetLayer(trans.GetChild(i), layer);
}
}
Answer by tmdchi · Jul 06, 2012 at 10:34 AM
gameObject.GetComponentsInChildren(Transform);
That will return an array of all the transforms under your gameObject.
Yes or
gameObject.GetComponentsInChildren<Transform>(); in C#
Please tell us what language you are using when asking a question so it is possible to give you the most relevant answer.
And all components on this gameobject as well. $$anonymous$$eep that in $$anonymous$$d ;)
You can of course get them recursively (or iteratively) by using a recursive function or a List, but since Untiy provides GetComponentsInChildren that's the easiest way.
example gets all children js:
var go = Selection.activeGameObject;
var allChildren = go.GetComponentsInChildren(Transform);
for (var child : Transform in allChildren) {
print(child.gameObject.name);
if(child.gameObject.GetComponent($$anonymous$$eshRenderer)){
child.gameObject.AddComponent($$anonymous$$eshCollider);
}
}
I like this answer, how would it be in C#? I'm getting some problems at translating it.
Assu$$anonymous$$g GameObject go;:
Transform[] allChildren = go.GetComponentsInChildren<Transform>();
foreach (Transform child in allChildren)
{
Debug.Log(child.gameObject.name);
if (child.gameObject.GetComponent<$$anonymous$$eshRenderer>() != null)
{
child.gameObject.AddComponent<$$anonymous$$eshCollider>();
}
}
Although tmdchi's answer is wrong for the OP question, please notice that in some situations - noticeably while doing DestroyImmediate editor commands - you might get problems with the standard GetComponentsInChildren method. In these cases a slight variation of what he wrote is the easiest/safest route:
while (gO.childCount > 0){
foreach (Transform child in gO) {
DestroyImmediate(child.gameObject);
}
}
foreach(Transform t in target.GetComponentsInChildren<Transform>(true)) //include inactive
{
}