Find children of children of children of... - Multi-Level Hierarchy Parsing

Hi All,
I want to find children gameobject from the parent gameobject. each children may or may not have their own child (gran children) and again each grand children may or may not have their child (great grand children) and so. Im importing a model @ runtime so i don’t know about the depth of the hierarchy.
For Example Something Like this…

Root GameObject
|_Children1
|_Child1 of Children1
|_Child2 of Children1
|_Child1 of Child2 of Children1
|_Children2
|_Child1 of Children2
|_Child1 of Child1 of Children2
|_and so on.
|_Child2 of Children2

FindComponentsInChildren will get you every single child.

If you want to step through one at a time in some form of order, transform is an iterator, so you can do a recursive search that will reach all children.

Right now i have fbx file in which the depth of hierarchy is 7. if i use recursive search will it find all the components from the root node. Could you give me a sample code of recursive search.

You can go through all transforms in the hierarchy like this:

void TraverseHierarchy(Transform t)
{
    // do stuff with t
   
    for(int i = 0; i < t.childCount; ++i)
    {
        TraverseHierarchy(t.GetChild(i));
    }
}

TraverseHierarchy(root); // pass your root transform
1 Like

Thank you villevli. ill try it out and let you know if i got the output.