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

Hi

Is there a way to find all GameObjects by using their tag even if some of them are inactive at the time?

I have multiple gameObjects with the same tag and if I click on one of them that object will become inactive. I then want to basically reset them all back to their original active state using a button of some sort.

To my knowledge the GameObject.FindGameObjectsWithTag(“”) function doesn’t work on inactive objects. Is there a way to do this in any other way? :slight_smile:

Thanks in advance

This question has already been answered.

There is no way to find inactive objects with GameObject.FindGameObjectsWithTag(“”) as you mentioned
but you can store inactive objects in an array and and get objects from array is much more performant than using GameObject.FindGameObjectsWithTag(“”) each time .

these links also answer same question:

public static class FindGameobjekts
{
public static List FindTagObjects(this GameObject parent, string tag)
{
List objs = new List();
foreach (Transform t in parent.GetComponentsInChildren(true))
{
if (t.tag == tag)
{
objs.Add(t.gameObject);
}
}
return objs;
}
}

Then you can search through scene root objects with it.

foreach (GameObject rootobj in SceneManager.GetActiveScene().GetRootGameObjects())
{
     foreach (GameObject _textobj in rootobj.FindTagObjects("TEXT"))
     {
          //Your Magic here
     }
}

Now you can catch also the inactive gameobjects with spezific tag.

I did something simpler, made an empty game object and set its’ tag to the tag, while modified the child’s active state, which is the required object.

GameObject[] flags = GameObject.FindGameObjectsWithTag("Flag");
        foreach (var flag in flags)
        {
            GameObject go = flag.transform.GetChild(0).gameObject;
            go.SetActive(true);
        }

my advice is never to destroy an object unless you are sure 100% that you will not recall it again.