x


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?

more ▼

asked Oct 23 '09 at 06:38 PM

Daniel Sperry 2 gravatar image

Daniel Sperry 2
662 11 15 30

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.

Dec 29 '10 at 06:41 AM yoyo
(comments are locked)
10|3000 characters needed characters left

4 answers: sort voted first

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.

more ▼

answered Oct 23 '09 at 09:49 PM

Lucas Meijer 1 gravatar image

Lucas Meijer 1 ♦♦
8k 19 43 85

(comments are locked)
10|3000 characters needed characters left

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);
  }
more ▼

answered Sep 15 '10 at 06:06 PM

CJCurrie gravatar image

CJCurrie
692 7 11 25

(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Dec 29 '10 at 06:40 AM

yoyo gravatar image

yoyo
6.4k 25 39 84

(comments are locked)
10|3000 characters needed characters left

And what must I do if I want to get ALL the objects of a given type, not only the ACTIVE ones?

more ▼

answered May 13 '10 at 09:48 PM

rlbaldi gravatar image

rlbaldi
159 3 5 10

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

May 13 '10 at 10:10 PM qJake

that won't return any inactive objects, which is what he's asking. I'd like to now that too :s

Jun 02 '12 at 06:25 PM Steven 1

Resources.FindObjectsOfTypeAll

Dec 13 '12 at 01:24 AM invadererik
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1677
x719

asked: Oct 23 '09 at 06:38 PM

Seen: 11279 times

Last Updated: Dec 13 '12 at 01:24 AM