Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 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
9
Question by BlackArcane · Oct 08, 2012 at 04:03 PM · c#scenegameobjectsgetin

How to get all GameObjects in Scene?

I need to get every single game object in the current scene in a GameObject[] using C#... Any ideas? Thanks in advance! :)

Comment
Add comment · Show 2
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 shaderop · Oct 08, 2012 at 04:16 PM -1
Share

http://lmgtfy.com/?q=Unity+find+all+game+objects+in+a+scene

avatar image slippdouglas · Apr 07, 2014 at 10:11 PM 8
Share

@shaderop: Which now leads back here!

7 Replies

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

Answer by justinl · Oct 08, 2012 at 04:16 PM

You could try either of these:

 GameObject.FindObjectsOfType(typeof(MonoBehaviour)); //returns Object[]
 GameObject.FindGameObjectsWithTag("Untagged");  //returns GameObject[]
Comment
Add comment · Show 5 · 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 TheDDestroyer12 · Sep 12, 2014 at 05:44 PM 4
Share

GameObject go = (GameObject) GameObject.FindObjectsOfType(GameObject);

avatar image pd491 · Aug 01, 2017 at 05:02 PM 0
Share

I strongly advice against that solution. Because in my case, it got Unity editor to the state where it freezes

avatar image Mikilo · Aug 01, 2017 at 05:27 PM 0
Share

Well, your scene must be heavy or broken. Because these functions are not harmful.

But I agree these functions are not particularly efficient.

avatar image pd491 · Aug 02, 2017 at 01:53 PM 0
Share

@$$anonymous$$ikilo, my scene is not that big actually. What can be broken about it?

avatar image Mikilo · Aug 02, 2017 at 03:09 PM 0
Share

@pd491 Consider these functions to be very dumb. The problem might be somewhere else. Ever tried an other scene?

avatar image
25

Answer by jonc113 · Mar 29, 2013 at 12:17 AM

I have found that FindObjectOfType will return all kinds of weird things that are not in the scene. "activeInHeirachy" solves the problem:

 GameObject[] allObjects = UnityEngine.Object.FindObjectsOfType<GameObject>() ;
 foreach(object go in allObjects)
    if (go.activeInHierarchy)
       print(thisObject+" is an active object") ;
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 justinl · Nov 21, 2013 at 09:21 PM 0
Share

Looks fine to me. Probably an extra unnecessary pair of brackets in the if statement but it's fine imho.

avatar image mprivat · Jan 19, 2015 at 03:08 PM 2
Share

Looks like it should be foreach(GameObject go in allObjects) so that you can safely call activeInHierarchy

avatar image SpacePilot1000 · Jan 21, 2015 at 04:16 PM 0
Share

UPDATE: It looks like in the current version of Unity (4.6) FindObjectsOfType() will return only active objects in the scene.

So you no longer need to check the activeInHierarchy flag on each object.

Here's the docs: http://docs.unity3d.com/ScriptReference/Object.FindObjectsOfType.html

avatar image ForceMagic · Jan 17, 2017 at 07:31 PM 0
Share

Yeah, that is not always true, the definition of ActiveInHierarchy is :

This lets you know if a gameObject is active in the game. That is the case if its GameObject.activeSelf property is enabled, as well as that of all it's parents.

which means that a gameObject could be in the Scene but not enabled and it would not handle that case.

In my case, I used Resources.FindObjectsOfTypeAll<GameObject>(): combined with

         if (!go.scene.isLoaded)
             continue;

in my go loop and it worked like a charm.

avatar image ForceMagic · Jan 17, 2017 at 07:49 PM 0
Share

Yeah, that is not always true, the definition of ActiveInHierarchy is :

This lets you know if a gameObject is active in the game. That is the case if its GameObject.activeSelf property is enabled, as well as that of all it's parents.

which means that a gameObject could be in the Scene but not enabled and it would not handle that case.

In my case, I used Resources.FindObjectsOfTypeAll<GameObject>(): combined with

      if (!go.scene.isLoaded)
          continue;

in my go loop and it worked like a charm.

Show more comments
avatar image
6

Answer by colinday · Dec 02, 2016 at 06:01 PM

 // 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 ];
     gameObject.DoSomething();
 }
Comment
Add comment · Show 5 · 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 Mikilo · Dec 19, 2016 at 08:36 AM 0
Share

Unfortunately this way does not work well. It does not handle disabled GameObject.

avatar image colinday · Dec 22, 2016 at 07:30 PM 0
Share

@$$anonymous$$ikilo, actually that's not correct ... it does handle inactive game objects, inactive game objects are returned from GetRootGameObjects() as well as active game objects. I just tested this myself in the debugger on Unity 5.5.0f3.

avatar image Mikilo · Dec 22, 2016 at 09:43 PM 0
Share

@colinday I am sorry, I pointed the wrong thing. It handles inactive GameObject. BUT, it does not handle GameObject with the state DontDestroyOnLoad on them.

Using Unity 5.4.0F3.

avatar image colinday · Dec 22, 2016 at 10:14 PM 1
Share

@$$anonymous$$ikilo, sure enough you're right on that one ... which to me clearly seems like a bug. I've submitted it to Unity through the bug reporter, hopefully they will include those objects in the call in a future update.

avatar image Mikilo · Dec 22, 2016 at 10:25 PM 0
Share

@colinday $$anonymous$$aybe, since these objects are not in a scene anymore, we need to look for them somewhere else, I am not sure it's a bug, but we need to figure it out where to find them.

Edit: I did more tests, these objects seem to be kind of orphans. Which is pretty bad.

avatar image
2

Answer by small-U · Nov 21, 2013 at 12:10 PM

C# code:

 Transform[] hinges =GameObject.FindObjectsOfType(typeof(Transform)) as Transform[];

javaScript Code:

 var hinges :Transform[] = FindObjectsOfType(Transform) as Transform[];
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
2

Answer by idbrii · Jan 29, 2018 at 07:19 PM

The Unity documentation for Resources.FindObjectsOfTypeAll has this example:

 // This script finds all the objects in scene, excluding prefabs:

 List<GameObject> GetAllObjectsInScene()
 {
     List<GameObject> objectsInScene = new List<GameObject>();

     foreach (GameObject go in Resources.FindObjectsOfTypeAll(typeof(GameObject)) as GameObject[])
     {
         if (go.hideFlags == HideFlags.NotEditable || go.hideFlags == HideFlags.HideAndDontSave)
             continue;

         if (!EditorUtility.IsPersistent(go.transform.root.gameObject))
             continue;

         objectsInScene.Add(go);
     }

     return objectsInScene;
 }


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
  • 1
  • 2
  • ›

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

25 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How can i find out how much element on scene now? 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

question about dynamic vs baked lighting 1 Answer

How do i activate a function attached to a listed GameObject from the perspective of itself? 1 Answer


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