- Home /
how to destroy all gameobjects with a certain tag?
Hello all. In my scene there is a timer that counts down from 60 seconds. (not a coroutine just a visible timer). When it reaches "0", I want a function to get all the gameobjects that are in the scene tagged as "Enemy" AND if there are 10 or less, DESTROY them all, BUT if there are 11 or more, do something else (stop the game etc.) I cant seem to get this to work properly and it doesn't seem like it should be that sophisticated. Thank you in advance and im using C#.
Answer by pako · Mar 08, 2018 at 08:34 PM
if (timer <= 0)
{
GameObject[] foundObjects = GameObject.FindGameObjectsWithTag("Enemy"); // NOTE: only ACTIVE gameObjects will be found
if (foundObjects.Length < 10){
foreach (GameObject go in foundObjects)
{
Destroy(go);
}
}
else
{
//do something else (stop the game etc.)
}
}
That is exactly what I was trying to do. Thank you so much @pako!!
Your answer
![](https://koobas.hobune.stream/wayback/20220612154203im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Trying to change scenes after objects are destroyed 1 Answer
Colliding Objects 1 Answer
Destory() on Collision if Tag = Lazer [SOLVED] 1 Answer
Destroy Objects after a set time 2 Answers