- Home /
How do I iterate over all scene objects from an editor script?
What is the best way to iterate over all scene objects from an editor script?
Supose I want to make a script extract some scene information.
Is this the best way?
object[] obj = GameObject.FindSceneObjectsOfType(typeof (GameObject)); foreach (object o in obj) { GameObject g = (GameObject) o; Debug.Log(g.name); }
Could GameObject.FindSceneObjectsOfType pick objects from other editors or other objects that are not in the current scene graph?
You can also just use "foreach (GameObject g in obj)" -- this will effectively perform the typecast for you, and throw an exception if it encounters an object that isn't a GameObject.
Answer by Lucas Meijer 1 · Oct 23, 2009 at 09:49 PM
That should work fine. In contrast to what you might expect, Unity has no "root node" which you can ask for its children. You have to do what you do now.
And what must I do if I want to get ALL the objects of a given type, not only the ACTIVE ones?
Don't ask a question as an answer (this isn't a forum). You'd want to use FindObjectsOfType() here: http://unity3d.com/support/documentation/ScriptReference/Object.FindObjectsOfType.html?from=GameObject
that won't return any inactive objects, which is what he's asking. I'd like to now that too :s
That includes prefabs - I'm still looking for a good way to do this.
This answer is outdated. This is correct now [http://answers.unity.com/answers/1279524/view.html]
Answer by colinday · Dec 02, 2016 at 06:07 PM
Now possible in Unity via.
// get root objects in scene
List<GameObject> rootObjects = new List<GameObject>();
Scene scene = SceneManager.GetActiveScene();
scene.GetRootGameObjects( rootObjects );
// iterate root objects and do something
for (int i = 0; i < rootObjects.Count; ++i)
{
GameObject gameObject = rootObjects[ i ];
doSomethingToHierarchy( gameObject );
}
Thanks a lot, this was useful for my own purposes of just operating on the root nodes of my scene. Since nobody else has said it, usually you never want to iterate over every object in your game. Aside from that I wanted to point out two changes i would make to this solution: 1) You're going to need to put "using UnityEngine.Scene$$anonymous$$anagement;" at the top of your script to use the "Scene" and "Scene$$anonymous$$anager" keywords. Or just do what I'm doing until I have time to do a full QA cycle, and prepend the entire namespace to each part that needs it. [See code below]
2) If you read on Unity's documentation, they mention that scene.GetRootGameObjects( rootObjects ); should have "rootObjects" already initialized to larger than "scene.rootCount". So I moved the lines around a bit to get the scene and find it's root object count. I don't know how much of an impact it makes especially if you're doing it one time but if you'd like to research further on the effects of Unity "allocating memory internally" vs the List declaration allocating memory on its own, it may or may not be significant. I'm doing it this way to follow the documentation's suggestions. Who knows how up to date it actually is.
// get root objects in scene
UnityEngine.Scene$$anonymous$$anagement.Scene scene = UnityEngine.Scene$$anonymous$$anagement.Scene$$anonymous$$anager.GetActiveScene();
List<GameObject> rootObjects = new List<GameObject>(scene.rootCount + 1);
scene.GetRootGameObjects(rootObjects);
// iterate root objects and do something
for (int i = 0; i < rootObjects.Count; ++i)
{
GameObject gameObject = rootObjects[i];
if(gameObject != null) { doSomethingToHierarchy(gameObject); }
}
Answer by CJCurrie · Sep 15, 2010 at 06:06 PM
GameObject doesn't actually contain a definition for FindSceneObjectsOfType(). You'll want to use
Type[] myVar = FindObjectsOfType (typeof(Type))
instead. For all GameObjects it's just
object[] obj = GameObject.FindObjectsOfType(typeof (GameObject));
foreach (object o in obj)
{
GameObject g = (GameObject) o;
Debug.Log(g.name);
}
Answer by yoyo · Dec 29, 2010 at 06:40 AM
The SceneDumper script on the wiki will iterate all the the selected objects and their children. Currently it just prints the object hierarchy and the names of each object's components, but it could be modified to print all the component properties, filter by type, etc.
Given a game object, you can find its top-most ancestor in the scene tree using Transform.root -- i.e. gameobj.transform.root.gameObject. But note that there can be multiple "root" nodes in a scene.