• 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
3
Question by oxium · Aug 19, 2011 at 06:58 AM · gameobjectactive

FindSceneObjectsOfType ignoring the inactive gameobjects.. why ?

Hi all

I need to get all the gameobjects in the scene even the inactive ones..

Any idea how I can do that ?

At the moment I use the FindSceneObjectsOfType function but it does give me only the active ones.

And it looks like there's no parameter to customize the function to return everything..

Any idea ? This sounds like something pretty easy / obvious so I hope someone found a solution or there's a workaround inside unity for this kind of need !

Thanks in advance for any help !

ps: I know this function is slow but this is for a tool so I don't really care about speed/perf ;)

Comment
Add comment
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

5 Replies

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

Answer by alexfeature · May 06, 2012 at 08:40 AM

I know this is a very very very old post but I was looking for a way to do what the OP wanted and found this in the reference docs.

FindObjectsOfTypeAll

Based on the description it should return every object even if its disabled.

Comment
Add comment · Show 4 · 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 ksa · Jul 26, 2012 at 10:11 PM 2
Share

It really includes all objects in memory including much of internal stuff not present in original scene. So if you can differentiate objects you need from others it will do the job.

avatar image whydoidoit · Jul 26, 2012 at 10:31 PM 1
Share

It's worth noticing that you can tell if an object was instantiated after the scene started by checking its GetInstanceID() which will be positive for things created dynamically and negative otherwise. Other than that as @ksa says there are a lot of things returned including editor objects, preview cameras etc.

avatar image KGC · Dec 03, 2015 at 02:26 PM 0
Share

Link is broken, heres an updated link: http://docs.unity3d.com/ScriptReference/Resources.FindObjectsOfTypeAll.html

avatar image Bunny83 KGC · Dec 03, 2015 at 03:12 PM 0
Share

Fixed the link, thanks ^^

avatar image
21

Answer by rempelj · Nov 16, 2016 at 07:26 AM

Resources.FindObjectsOfTypeAll loads project assets, including prefabs, which can cause problems especially if you want to modify the results. As an alternative to Resources.FindObjectsOfTypeAll, you can now use Unity's Scene Manager to find objects, like so:

 /// Use this method to get all loaded objects of some type, including inactive objects. 
 /// This is an alternative to Resources.FindObjectsOfTypeAll (returns project assets, including prefabs), and GameObject.FindObjectsOfTypeAll (deprecated).
 public static List<T> FindObjectsOfTypeAll<T>()
 {
     List<T> results = new List<T>();

     for(int i = 0; i< SceneManager.sceneCount; i++)
     {
         var s = SceneManager.GetSceneAt(i);
         if (s.isLoaded)
         {
             var allGameObjects = s.GetRootGameObjects();
             for (int j = 0; j < allGameObjects.Length; j++)
             {
                 var go = allGameObjects[j];
                 results.AddRange(go.GetComponentsInChildren<T>(true));
             }
         }
     }

     return results;
 }

This is useful if you want to modify loaded objects without affecting prefabs and other assets.

Note: You can replace SceneManager with EditorSceneManager if needed.

Last tested in Unity 5.6.0.

Comment
Add comment · Show 9 · 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 tafer95 · Mar 06, 2017 at 06:38 PM 0
Share

It works well! thanks for share

avatar image MrLucid72 · Jul 09, 2017 at 02:40 PM 0
Share

@rempelij I am getting the following err when I implemented this:

 ArgumentException: GetComponent requires that the requested component 'GameObject' derives from $$anonymous$$onoBehaviour or Component or is an interface.

avatar image rempelj MrLucid72 · Jul 09, 2017 at 03:47 PM 0
Share

@dylanh7244 I just tested this in Unity 5.6.0 and there were no errors. Did you modify the function?

avatar image snoopyuj · Oct 11, 2017 at 03:50 AM 0
Share

This solution is great! But it can't find the objects in "DontDestroyOnLoad" scene. Still can't find the perfect solution :(

avatar image misher · Apr 16, 2018 at 02:23 PM 1
Share

Oneliner:

 public static List<T> FindObjectsOfTypeAll<T>()
 {
     List<T> results = new List<T>();      
     Scene$$anonymous$$anager.GetActiveScene().GetRootGameObjects().ToList().ForEach(g => results.AddRange(g.GetComponentsInChildren<T>()));
     return results;
 }

avatar image Mr_Mow misher · Jan 09, 2019 at 03:56 PM 0
Share

Update of best answer:

 public static List<T> FindObjectsOfTypeAll()
 {
     List<T> results = new List<T>();      
         UnityEngine.Scene$$anonymous$$anagement.Scene$$anonymous$$anager.GetActiveScene().GetRootGameObjects().ToList().ForEach(g => results.AddRange(g.GetComponentsInChildren<T>(true)));
         return results;
     }
avatar image MrXo misher · Jan 04, 2020 at 09:55 AM 0
Share

Even shorter:

 public static List<T> FindObjectsOfTypeAll<T>()
 {
     return Scene$$anonymous$$anager.GetActiveScene().GetRootGameObjects()
         .Select$$anonymous$$any(g => g.GetComponentsInChildren<T>(true))
         .ToList();
 }

avatar image whitesundreams MrXo · Feb 12, 2020 at 02:25 AM 0
Share

Hi $$anonymous$$rXo can you give me an example of how I can use this, I tried to use the tried and true copy paste method, but it's giving me an error.

$$anonymous$$y code is below

 List<GameObject> gameObjectList = FindObjectsOfTypeAll<GameObject>();
 
 public static List<T> FindObjectsOfTypeAll<T>()
     {
         return Scene$$anonymous$$anager.GetActiveScene().GetRootGameObjects()
             .Select$$anonymous$$any(g => g.GetComponentsInChildren<T>(true))
             .ToList();
     }

It compiles and runs fine, however when it does that. I get an exception thrown

"ArgumentException: GetComponent requires that the requested component 'GameObject' derives from $$anonymous$$onoBehaviour or Component or is an interface."

What is causing this error for me?

Thanks

Show more comments
avatar image
2

Answer by Senhor de todo o Mal · Aug 19, 2011 at 08:55 AM

You can't.

Either you:

  • Keep a reference to all the inactive objects and access them through the reference

  • Or you create a root gameobject and parent all the other gameobjects in the scene to it, then use GetComponentsInChildren(Transform,true) on it and iterate through all the components returned calling .gameObject on each one

Comment
Add comment · Show 9 · 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 oxium · Aug 22, 2011 at 01:56 AM 0
Share

thanks..

the first solution wouldn't work in my case but I guess I can do what you suggest with the root gameobject...

but still I'd to know (from unity point of view) what the interest of having this function ignoring the inactive gameobjects..

would that be possible to add in 3.5 that is coming pretty soon, a bool parameter to this function so we can specify if we want all the gameobjects or not (or another function.. whatever..)

it doesn't seem to be a big change..

any idea how to request a new feature ?

are the awesome unity coders coming on unity answers sometimes ?

avatar image oxium · Aug 22, 2011 at 01:58 AM 0
Share

to be a bit more precised I'm working on a tool so I don't really want to force all my future users to create an empty root gameobject otherwise the tool won't work.. it would be a bit ugly..

avatar image Joshua · Aug 22, 2011 at 01:59 AM 0
Share

$$anonymous$$ake a wish: http://feedback.unity3d.com/forums/15792-unity

The reason it ignores inactive gameobjects? Because, by design, inactive gameobjects don't exist for all intends and purposes, other then to be activated by something that holds their reference. It'd be stupid if they weren't ignored by these kinds of functions, because then you'd have to check for .active and the function would be significantly slower.

I probably shouldn't but.. "the first solution wouldn't work in my case".. why? That probably means you're approaching this wrong.

avatar image oxium · Aug 22, 2011 at 02:41 AM 0
Share

hum..

ok basically I'm working on some sort of visual editor and I need to access all the gameobjects of the current scene (active or not) so I can edit them directly, group them etc.. and also activate / inactivate them..

the only way I've found is via the FindSceneObjects.. function

is there another way to get a reference on gameobjects ?

avatar image ksa · Jul 20, 2012 at 09:03 PM 0
Share

I have the same problem. Are there any updates on this? The problem was mentioned earlier. I want to have editor script that will do something with objects on the scene (both enabled and disabled). How to access the list of all game objects?

Show more comments
avatar image
2

Answer by MrLucid72 · Jul 09, 2017 at 05:16 AM

HEADS UP guys -- use rempelj 's answer above (NOT the best answer)

Use SceneManager and not Resources because there's currently multiple unaddressed bugs that will PERMANENTLY change your prefabs and scene!

Posting as an answer to get more attention because this is some really serious stuff.

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
0

Answer by ksa · Jul 26, 2012 at 10:33 PM

 var currentlyenabled = GameObject.FindWithTag ("enabled");
 var currentlydisabled = GameObject.FindWithTag ("disabled");

In theory this plus FindGameObjectsWithTag equivalent should work. I tested it and it currently kills Unity Editor. However a guy from Unity promised it should be fixed in Unity 3.5.5

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

19 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

Related Questions

If you disable a gameobject, does an InvokeRepeating loop end or pause? 3 Answers

Script only works onetime 0 Answers

How to make an open and close box?,How to make open and close a box? 0 Answers

activate GameObject dont work 1 Answer

"Prefab GameObject's can not be made active!" - Need a fix 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges