Clearing the screen of Enemies

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

Can anyone help explain why this is not working?

1 Like

Here you can see that they are tagged correctly:

1 Like

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

1 Like
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.

1 Like

I would concur…

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);
}

2 Likes

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.

3 Likes

Nice one @OboShape, Awesome! :+1:

1 Like

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.

2 Likes

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

1 Like

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

Privacy & Terms