18

I have a gameobject which I wish to activate given a certain condition. I gave it a unique tag and I tried using GameObject.FindObjectWithTag("Tag name"). From what I can tell, this method will only find active gameobjects in the scene and not inactive ones.

Is there a method that I can call that will also search inactive gameobjects? (Preferably searching by tag).

Thanks!

0

3 Answers 3

27

After some research it seems that there is no way to find an inactive gameobject by tag.

solutions exist however to access inactive gameobjects:

1 - Store inactive game objects in an array if you need to reactivate them afterwards (only applies to game objects inactivated at runtime).

2 - Do not deactivate game object, simply deactivate the components you want inactive. If you wish to make the object disappear, deactivate the renderer. If it is a specific script, deactivate that script, etc.

This solution will allow you to still find a game object by its tag name.

3
  • mark it as right, because it is the right answer indeed.
    – Heisenbug
    Apr 23, 2013 at 12:57
  • Sure is right. Can't find inactive object. Must store reference. Apr 23, 2013 at 14:20
  • This is wrong. You can get inactive objects. See comment below. RTFM.
    – Alex
    Jun 14, 2013 at 5:41
3

Things that can find inactive gameObjects :

transform.Find() or transform.FindChild()
transform.GetComponentsInChildren<Component>(true)

Resources.FindObjectsOfTypeAll<Component>()
4
  • @kleopatra : Sry. ... i'll keep this in mind . :)
    – Vineet
    Jul 25, 2015 at 8:29
  • While this can find inactive objects, it won't find objects by Tag, which was what I was actually asking.
    – kUr4m4
    Jun 10, 2016 at 23:05
  • Do not use Resources.FindObjectsOfTypeAll<Component>() unless you know what you're doing. This effects ANYTHING in resources, including prefabs. This means you will make permanent changes to your files and anything outside the scene, even after you stop playing.
    – dylanh724
    Jul 9, 2017 at 14:26
  • DO NOT CLICK ON THAT "find more details" links its domain is gone now spam Mar 17, 2019 at 22:23
1

FindObjectsOfTypeAll does find inactive, it just may also find prefabs and things you are not looking for, so you have to be careful.

1
  • Yes, and it is very slow
    – RedOrav
    Jul 4, 2013 at 13:23

Not the answer you're looking for? Browse other questions tagged or ask your own question.