- Home /
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? :)
Thanks in advance
Answer by Atiyeh123 · Sep 04, 2018 at 12:06 PM
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:
https://answers.unity.com/questions/61174/is-there-a-findgameobjectswithtag-that-returns-act.html https://answers.unity.com/questions/1202627/how-to-access-an-inactive-gameobject-with-tag.html
Answer by Playbosh-Games · Dec 03, 2020 at 01:19 PM
public static class FindGameobjekts
{
public static List<GameObject> FindTagObjects(this GameObject parent, string tag)
{
List<GameObject> objs = new List<GameObject>();
foreach (Transform t in parent.GetComponentsInChildren<Transform>(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.
Answer by Cristella · May 20, 2021 at 03:02 PM
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);
}
Answer by Adil_Alhilali · May 20, 2021 at 03:18 PM
my advice is never to destroy an object unless you are sure 100% that you will not recall it again.
Your answer
Follow this Question
Related Questions
GameObject is not activating from StateMachineBehavior script 0 Answers
Help Activating/Deactivating all Game Objects with same tag 1 Answer
How to find Inactive GameObject 16 Answers
GameObject.Find() work on inactive objects 16 Answers
Toggling a game object between an active and inactive state 1 Answer