• 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
40
Question by ina · Mar 16, 2011 at 07:53 AM · gameobjectobjectsfindactiveinactive

GameObject.Find() work on inactive objects

Does GameObject.Find() work on inactive objects? If not, how do you find and reference inactive objects.

Comment
Add comment · Show 7
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 ina · Mar 16, 2011 at 08:19 AM 1
Share

i take it from that upvote this isn't going to be an easy solution? :-\

avatar image FLASHDENMARK · Mar 16, 2011 at 01:06 PM 0
Share

Proberly not :)

avatar image Quazistax · Apr 18, 2011 at 05:22 PM 12
Share

It seems like one of those strange design side effects that they don't have time or will to change. If you don't keep a reference to your inactive object it disappears in abyss of Unity's internal mystery.

I actually heard a rumor that they live happy lives there, free, with no one looking over their shoulders, no one trying to control them... they just keep dancing barefoot around fire in warm nights... But none of them ever returned to confirm that story. :-\

Here is example of one workaround: http://answers.unity3d.com/questions/14178/cannot-toggle-active-on-gameobjects-that-are-inactive

avatar image Wolfram · Jun 07, 2011 at 10:32 PM 2
Share

Please search Unity Answers and/or the forum, this question has been asked a gazillion times, and there have been many exhaustive answers given on how to find inactive objects. I remember giving a lengthy, detailed answer to at least one of these questions here on Unity Answers.

avatar image Wolfram · Jun 07, 2011 at 10:48 PM 1
Share

This lists several alternatives dealing with the problem: http://answers.unity3d.com/questions/125847/trouble-accessing-child.html

Show more comments

16 Replies

· Add your reply
  • Sort: 
avatar image
9

Answer by Antony-Blackett · Jun 03, 2011 at 04:51 AM

The solution only works if you don't rely on transform.root anywhere in your game. It also doesn't work if you need to find objects that are children of objects that don't get destroyed on load.

Add a GameObject that is the root of all GameObjects in your scene. Then instead of

 FindObjectsOfType() 

use

 GetComponentsInChildren( typeof(Transform), true );

That should return all transforms of all GameObjects in the scene as they are all a child of the scene root, active or not.

If you can't or don't want to create a scene root object then you'll need to make an array of all the root objects in your scene and all GetCompoenentsInChildren() on all of them separately.

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 yoyo · Jun 07, 2011 at 10:11 PM 1
Share

nice hack, but it's still a cheesy workaround for a missing feature that ought to be included :-p

avatar image Antony-Blackett · Jun 08, 2011 at 12:22 AM 0
Share

indeed it is.

avatar image Freddy-Mesa · Sep 02, 2014 at 03:03 PM 0
Share

Thanks! good help!

avatar image segafreak1999 · Jan 09, 2015 at 06:09 AM 0
Share

And if you are wanting to find an inactive by name, you can do this:

 Text[] texts = m_rewardPopup.GetComponentsInChildren<Text>(true);
 Text plus = System.Array.Find(texts, (search) => (search.name.Equals("plus", System.StringComparison.Ordinal)));
avatar image Noob_Vulcan · Jul 23, 2015 at 11:04 AM 0
Share

For more u can refer http://www.unityrealm.com/how-to-find-inactive-gameobject-in-unity/

avatar image
4

Answer by Antony-Blackett · Dec 05, 2011 at 04:08 AM

Here's how you do it.

Resources.FindObjectsOfTypeAll(typeof(GameObject)) as GameObject[];

Note this can also return prefabs not just object instances in the scene so be careful when using it at edit time.

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
3

Answer by bdawson · Oct 31, 2012 at 09:30 PM

This is kind of gross, but it works well:

     public static List<GameObject> GetAllObjectsInScene(bool bOnlyRoot)
     {
         GameObject[] pAllObjects = (GameObject[])Resources.FindObjectsOfTypeAll(typeof(GameObject));
 
         List<GameObject> pReturn = new List<GameObject>();
 
         foreach (GameObject pObject in pAllObjects)
         {
             if (bOnlyRoot)
             {
                 if (pObject.transform.parent != null)
                 {
                     continue;
                 }
             }
 
             if (pObject.hideFlags == HideFlags.NotEditable || pObject.hideFlags == HideFlags.HideAndDontSave)
             {
                 continue;
             }
 
             if (Application.isEditor)
             {
                 string sAssetPath = AssetDatabase.GetAssetPath(pObject.transform.root.gameObject);
                 if (!string.IsNullOrEmpty(sAssetPath))
                 {
                     continue;
                 }
             }
             
 
             pReturn.Add(pObject);
         }
 
         return pReturn;
     }
Comment
Add comment · Show 2 · 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 Brenden-Frank · Nov 11, 2012 at 05:48 AM 0
Share

Isn't AssetDatabase an editor class? $$anonymous$$eaning this script doesn't work as a runtime solution?

avatar image bdawson · Nov 12, 2012 at 02:57 PM 0
Share

Its true. You don't need that check unless you are in the editor. I will update.

avatar image
3

Answer by cregox · May 14, 2013 at 08:48 PM

There are at very least 4 options: `GetChild`, `GetComponentsInChildren`, `FindObjectsOfType` and `FindGameObjectsWithTag`. We could also use some variations that won't bring an array, for instance. Listed below in no particular order.

  1. GetChild a method of Transform, it still works even today on Unity4, and it's still not in the Docs for whatever reason. It will bring the child be it active or not. Probably the cheapest to use, since we have to use in each immediate parent we want to get the inactive GameObject. It will just be a pain to code and maintain.

  2. GetComponentsInChildren needs to have a root object. We could set up the scene with just 1 main root here. Very simple to use, but also very heavy. Apply this in the root object:

      foreach (Transform child in GetComponentsInChildren(true))`
    
    
  3. FindObjectsOfType doesn't need a root object. Docs say it won't return inactive objects, but it does. Since it's the heaviest listed here and highly unadvised to use, we could do something like:

      foreach ( GameObject root in GameObject.FindObjectsOfType(typeof(GameObject)) ) {
             if (root.transform.parent == null) { // game object with no parent
                 // here, iterate through each `root` child using your prefered method
                 // or simply remove the 'if' above
             }
         }
    
    
  4. FindGameObjectsWithTag my favourite one, way faster and simpler than all others, but we can't add more than 1 tag per object so it may be prohibitive if we already use tag for something else. It also needs no root, use it from anywhere:

      foreach ( GameObject obj in GameObject.FindGameObjectsWithTag("tag") )
    
    
    

Disclaimer: @bdawson actually gave the same solution about FindObjectsOfType and @Antony scratched it, twice. None were specific enough.

Comment
Add comment · Show 2 · 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 Fattie · Mar 16, 2017 at 07:08 PM 1
Share

in the current version of Unity (2017) FindObjectsOfType correctly DOES NOT return inactive objects. (Unfortunately!)

avatar image erdogan1 · Jan 05, 2018 at 09:46 PM 0
Share

In Unity 2017 "FindGameObjectsWithTag" doesn't find inactive objects.

avatar image
3

Answer by Noob_Vulcan · Aug 13, 2015 at 08:18 AM

Things that can find inactive gameObjects :

transform.Find() or transform.FindChild()

transform.GetComponentsInChildren(true)

Resources.FindObjectsOfTypeAll()

For more detail you can refer to http://www.unityrealm.com/how-to-find-inactive-gameobject-in-unity/

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
  • 3
  • 4
  • ›

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

22 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

Related Questions

How to find Inactive GameObject 16 Answers

Organize around inactive GameObjects not being findable 0 Answers

Finding all GameObjects with the same tag when some of them are inactive? 4 Answers

gameObject.find doesn't work 1 Answer

Activate gameobject that is deactivated with c# 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