Get all items from hierarchy?

In editor how can I get a list of all items in the hierarchy and possibly refer to each one?

For example create a list from all objects in a text file. I know the text part, but not the get hierarchy list.

Thanks

You can find all game objects like this:

foreach (GameObject obj in Object.FindObjectsOfType(typeof(GameObject)))
{
    Debug.Log(obj.name);
}

If you want to traverse down the hierarchy from a specific object, do something like this:

void Traverse(GameObject obj)
{
    Debug.Log(obj.name);
    foreach (Transform child in obj.transform)
    {
        Traverse(child.gameObject);
    }

}

To traverse the whole scene from the root, combine the above into something like this:

foreach (GameObject obj in Object.FindObjectsOfType(typeof(GameObject)))
{
    if (obj.transform.parent == null)
    {
        Traverse(obj);
    }
}

@ProbePlayer Oh sorry i didnt saw that. No my code is not like that.But i am pretty sure it works like that.I didnt saw that Traverse function you mentioned.Anyways i found that i dont even need it xD :slight_smile: But yea this is great way to do it.Thanks :slight_smile: