You can find a single root object like this (C#):
Transform xform = UnityEngine.Object.FindObjectOfType<Transform>();
GameObject rootObject = xform.root.gameObject;
Note that this calls FindObjectOfType rather than FindObjectsOfType, so it's faster *[EDIT: Or so you would expect, *however* FindObjectOfType actually calls FindObjectsOfType behind the scenes, so it's actually just as slow]*. However if your scene has more than one root object it will only find one of them.
So ... anyone know how to find all the root objects efficiently?
This works, but uses FindObjectsOfType, which the docs warn is slow, plus it needs to allocate an array big enough to hold all the transforms in the scene. I would like to know if there's a better way:
List<GameObject> rootObjects = new List<GameObject>();
foreach (Transform xform in UnityEngine.Object.FindObjectsOfType<Transform>())
{
if (xform.parent == null)
{
rootObjects.Add(xform.gameObject);
}
}