Merry Christmas! I was trying to search for multiple GameObjects with a specific tag and set them all active at once. I cannot seem to work it.
void FindObjects(){
GameObject[] object;
object = GameObject.FindObjectsWithTag("Archived");
object[0].setActive(true);
}
CodePudding user response:
void FindObjects()
{
GameObject[] objects;
objects = GameObject.FindGameObjectsWithTag("Archived");
if (objects.Length == 0)
{
Debug.Log("No game objects are tagged with 'Archived'");
}
else
{
for (int i = 0; i < objects.Length; i )
{
objects[i].SetActive(true);
}
}
}
CodePudding user response:
GameObject.FindObjectsWithTag Only finds active GameObjects, which means you don't do anything activating them.
You should use Resources.FindObjectsOfTypeAll which finds inactive GameObjects too. But beware, as it also finds loaded assets, not only GameObjects. Something like this would do the trick:
Transform[] objs = Resources.FindObjectsOfTypeAll<Transform>() as Transform[];
for (int i = 0; i < objs.Length; i ){
if (objs[i].hideFlags == HideFlags.None){
if (objs[i].gameObject.CompareTag("Archived")){
objs[i].gameObject.SetActive(true);
}
}
Little detail: If you encounter performance problems (thousands of objects being checked). Try adding the Objects to a List<> when creating them so you can just use the list instead of searching for the objects.