Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

find inactive gameobject by tag in unity3d [duplicate]

Tags:

c#

unity3d

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!

like image 355
kUr4m4 Avatar asked Apr 23 '13 11:04

kUr4m4


3 Answers

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.

like image 116
kUr4m4 Avatar answered Nov 19 '22 19:11

kUr4m4


Things that can find inactive gameObjects :

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

Resources.FindObjectsOfTypeAll<Component>()
like image 38
Vineet Avatar answered Nov 19 '22 19:11

Vineet


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

like image 1
sage Avatar answered Nov 19 '22 20:11

sage