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
25
Question by Exeneva · Feb 01, 2015 at 09:34 AM · gameobjectgameobject.findinactive

Find an inactive game object

I'm working on a parallel worlds 2D platformer. At the start of the game, I set one world to inactive (all of the world's objects are parented to an empty gameobject). Is there a way to grab the inactive game object and set it to active, for when the player wants to switch worlds? GameObject.Find() does not find a game object that is inactive, so I'm looking for a way to handle this.

Comment
Add comment · Show 4
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 $$anonymous$$ · Feb 01, 2015 at 09:40 AM 0
Share

If inactive means the gameobjects that aren't moved then possibly you can check if the gameobject's present transform is equal to it's transform when the scene started . If yes then Find the gameobject with it's tag else ignore.

avatar image fafase · Feb 01, 2015 at 09:44 AM 0
Share

I think he meant inactive as SetActive(false).

avatar image Exeneva · Feb 01, 2015 at 09:46 AM 0
Share

Right, that's what I meant, inactive as in SetActive(false).

avatar image donrigo · Oct 01, 2020 at 09:02 AM 0
Share

I know its late but i had to post my solution for you as well since i have been struggeling with this to. so my solution was to simply have the objects setactive(true) on awake, get the reference and then SetActive(false) right afterwards.

12 Replies

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

Answer by Windud · Jan 29, 2018 at 11:39 AM

Resources.FindObjectsOfTypeAll works on inactive GameObjects as well. You may be able to use Resources.FindObjectsOfTypeAll() to find all game objects.

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 LazyKnight · Feb 13, 2018 at 04:34 PM 4
Share

Oh my god, I can't believe they didn't support this simple, essential and frequent feature even in 2018...?!?!?!

for additional information, see here, it's a tutorial about Resources.FindObjectsOfTypeAll:

https://answers.unity.com/questions/158172/findsceneobjectsoftype-ignoring-the-inactive-gameo.html

I usually use it like this way for find a unique class:

 var fooGroup = Resources.FindObjectsOfTypeAll<AnUniqueClass>();
 if (fooGroup.Length > 0) {
   var foo = fooGroup[0];
 }
avatar image fafase LazyKnight · Apr 09, 2018 at 07:21 PM 2
Share

That's got to be a real wrong name choice here. I would never expect to look into that class. Resources tells me it deals with resources folder, so how come this find them in the scene??!! Is that another intern job at Unity? Anyway, it does the job indeed.

avatar image guetta18 LazyKnight · Aug 05, 2018 at 08:46 PM 1
Share

still watch out about performence...

avatar image Addyarb · Nov 11, 2018 at 08:40 PM 3
Share

Careful with this approach!

If you use it and have prefabs in your scene that get referenced, it will call the method you reference on the prefab itself as well as the object in the scene. That could change it in ways that won't be changed back when you stop running the game.

avatar image echoAndecho Addyarb · Jul 22, 2021 at 06:28 AM 0
Share

Can't agree more. Be very careful with this method!

I got confused when tweaking a scene object preview tool for designers. My approach is storing all objects that need to preview in a single parent object so that I can use GetComponentsInChildren to get them.

avatar image mokhabadi · Dec 14, 2020 at 03:49 PM 2
Share

don't use this method. it has permanent effect on your assets. find another solution.

avatar image
29

Answer by fafase · Feb 01, 2015 at 09:40 AM

Either a reference to the object before setting it off or create your own method since you have them all under the same parent object you can use GetComponentsInChildre

 public static GameObject FindObject(this GameObject parent, string name)
 {
     Transform[] trs= parent.GetComponentsInChildren<Transform>(true);
     foreach(Transform t in trs){
         if(t.name == name){
              return t.gameObject;
         }
     }
     return null;
 }


this is an extension method that should be stored in a static class. You use it like this:

 GameObject obj = parentObject.FindObject("MyObject");

Edit:

It appears Unity had the bright idea to add a clean solution under the Resources class:

https://docs.unity3d.com/ScriptReference/Resources.FindObjectsOfTypeAll.html

Comment
Add comment · Show 7 · 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 Exeneva · Feb 01, 2015 at 09:46 AM 0
Share

Thanks! I ended up just grabbing them on Awake in a levelmanager object and just referencing them before they are destroyed.

avatar image Jack1011 · Sep 19, 2017 at 09:25 AM 0
Share

Cannot implicitly convert type UnityEngine.Component[]' to UnityEngine.Transform[]'. An explicit conversion exists (are you missing a cast?)

avatar image fafase Jack1011 · Sep 19, 2017 at 02:10 PM 0
Share

I edited to use generic ins$$anonymous$$d.

avatar image RareRooster · Oct 04, 2017 at 10:39 AM 2
Share

Thanks @fafase this was just the answer I needed.

I only needed this a single time so, ins$$anonymous$$d of creating an extension method, I just used a little System.Linq.

 Transform childTransform = gameObject.transform.GetComponentsInChildren<Transform>(true).FirstOrDefault(t => t.name == "Name Of Child Object");

avatar image DannyBravo · Apr 08, 2018 at 04:07 AM 0
Share

Perfect; just what I was looking for. I like RareRooster's LINQ fix as well; good stuff.

avatar image Ziplock9000 · Nov 17, 2018 at 06:30 AM 0
Share

"Either a reference to the object before setting it" Well that's not finding it then, which is what the OP asked.

avatar image fafase Ziplock9000 · Nov 19, 2018 at 12:20 PM 0
Share

"[...] setting it or create your own method since you have them all under the same parent object you can use GetComponentsInChildren". Sometimes, OP asks for something that could have been done easier in a different manner. So it is common to give an answer that does not answer the question but more likely provide a different (sometimes more suitable) solution to the problem. In this case, 3 years ago (time goes by so fast), I was saying that there is this or there is an answer.

avatar image
8

Answer by DrtyDee · Sep 19, 2015 at 03:32 AM

If the object has a root, you can get a hold of the root and transform.Find the transform associated with the inactive object by name.

inactiveObject = rootObject.transform.Find( "nameOfInactiveObject" ).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 pandolfini · Aug 19, 2020 at 01:28 PM 0
Share

I was inadvertently doing this and had to come here just to confirm that this was "supposed" to work. This is a great option in 2020.

avatar image
6

Answer by Projectile_Entertainment · Feb 15, 2018 at 05:32 AM

I'm late to this party, and others, but I had this question myself, so for others' sake, here's what I found:

The docs here: https://docs.unity3d.com/ScriptReference/Resources.FindObjectsOfTypeAll.html have a pretty good method called GetAllObjectsInScene().

That said, I would personally change it to this: private static List GetAllObjectsInScene() { List objectsInScene = new List();

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

             if (PrefabUtility.GetPrefabType(go) == PrefabType.Prefab || PrefabUtility.GetPrefabType(go) == PrefabType.ModelPrefab)
                 continue;

             objectsInScene.Add(go);
         }
         return objectsInScene;
     }

Works in editor

Gets inactive objects

Doesn't grab hidden objects

Doesn't grab prefabs

Doesn't grab transient objects

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
4

Answer by MariuszKowalczyk · Jun 17, 2019 at 09:13 PM

Here is the proper solution:

     public static List<GameObject> FindAllObjectsInScene()
     {
         UnityEngine.SceneManagement.Scene activeScene = UnityEngine.SceneManagement.SceneManager.GetActiveScene();
 
         GameObject[] rootObjects = activeScene.GetRootGameObjects();
 
         GameObject[] allObjects = Resources.FindObjectsOfTypeAll<GameObject>();
 
         List<GameObject> objectsInScene = new List<GameObject>();
 
         for (int i = 0; i < rootObjects.Length; i++)
         {
             objectsInScene.Add(rootObjects[i]);
         }
 
         for (int i = 0; i < allObjects.Length; i++)
         {
             if (allObjects[i].transform.root)
             {
                 for (int i2 = 0; i2 < rootObjects.Length; i2++)
                 {
                     if (allObjects[i].transform.root == rootObjects[i2].transform && allObjects[i] != rootObjects[i2])
                     {
                         objectsInScene.Add(allObjects[i]);
                         break;
                     }
                 }
             }
         }

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

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

31 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to activate all inactive objects? 1 Answer

inactive objects impact on memory 1 Answer

Object reference not set to an instance of an object? 0 Answers

Find GameObjects with a true boolean and put them in an array? 1 Answer

Gets object with a script 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