Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
18
Question by Daniel Sperry 2 · Oct 23, 2009 at 06:38 PM · editorscene

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?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image yoyo · Dec 29, 2010 at 06:41 AM 1
Share

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.

4 Replies

· Add your reply
  • Sort: 
avatar image
11
Best Answer

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.

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image rlbaldi · May 13, 2010 at 09:48 PM 0
Share

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

avatar image qJake rlbaldi · May 13, 2010 at 10:10 PM 1
Share

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

avatar image Steven-1 rlbaldi · Jun 02, 2012 at 06:25 PM 0
Share

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

avatar image invadererik rlbaldi · Dec 13, 2012 at 01:24 AM 0
Share

Resources.FindObjectsOfTypeAll

avatar image Time-Jockey invadererik · Feb 17, 2016 at 01:45 AM 0
Share

That includes prefabs - I'm still looking for a good way to do this.

avatar image KingPeas_1 · Sep 25, 2018 at 06:31 AM 0
Share

This answer is outdated. This is correct now [http://answers.unity.com/answers/1279524/view.html]

avatar image
7

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 );
  }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AuggoDoggo · Aug 25, 2017 at 06:27 PM 1
Share

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

avatar image
4

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);
  }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

unity: scene shows nothing, PLEASE HELP 1 Answer

Cannot select multiple items in hierarchy / scene? 6 Answers

Accessing a UnityEditor class. 1 Answer

Raycast from mouse position in editor scene view? 1 Answer

Ho do I find all scenes with a certain script on an GameObject 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges