Clearing the screen of Enemies


#1

After 60 seconds I want all the Enemies on the screen.
Here are the routines:

Can anyone help explain why this is not working?


#2

Here you can see that they are tagged correctly:


#3

Are you receiving any compile or runtime errors or are the enemies just not disappearing?


#4
Destroy (GameObject.FindWithTag("Enemies"));

If i am right the way you are doing it is its looking for a gameobject called Enemies not its tag which it wont find.


#5

I would concur…


#6

Does this script find all enemies or just one? I cant try it right now. Perhaps you would have to use it with foreach as well, but I don`t know.

I think that you should use something like:

GameObject enemy = GameObject.FindGameObjectsWithTag(“Enemies”);
foreach(GameObject x in enemy)
{
Destroy(x);
}


#7

Yep, you will need to find all objects with that tag.
FindGameObjectsWithTag will return an array of type GameObjects as far as im aware;

        GameObject [] foundEnemies = GameObject.FindGameObjectsWithTag("Enemies");
	Debug.Log("Number of enemies found : " + foundEnemies.Length);

	if(foundEnemies.Length != 0)
	{
		foreach( GameObject enemy in foundEnemies)
		{
			Destroy(enemy);
		}
	}

so adding that would create an array, then allocate all enemies found with that tag,
and as Joao said, a ForEach to iterate through it and mark each game object element to be destroyed at the end of the frame.


#8

Nice one @OboShape, Awesome! :+1:


#9

Thanks for the help. Here is what I have learned

What the code does is search for something called “Enemies” in the hierarchy and destroy it, and all its children.
So if I could instantiate all my enemies under “Enemies” I would have my solution.


#10

If i remember rightly again zombie runner covers instantiating objects as a child of an empty gameobject which in your case you name Enemies.


#11

Hey @RetroDan - glad to hear you’ve got it sorted… could you mark the post as solved - thanks in advance :slight_smile: