How to destroy all objects with a certain tag

Hi guys I’m coding a small game in which I want the user to be able to click an emergency button to kill all enemies on screen

var destroyus : String ;


//var thatwasclicked : GameObject;
function OnMouseOver () 
{
 if (Input.GetMouseButtonDown(0))
 {
//find.GameObject.tag(destroyus) "
{ Destroy gameobject.tag(destroyus);

             	print("objects with tag should be gone ");
        	}
        }

This is the code I have thus far, and its to be applied to the game object the user clicks to clear the screen . Any hints on why it wont compile would be greatly appreciated

2 Likes

You want to do something like:

GameObject[] gos = GameObject.FindGameObjectsWithTag(destroyus);
foreach(GameObject go in gos)
     Destroy(go);

In the future, when you want help with an error, you probably want to post the error that is giving you trouble.

7 Likes

What you’re going to need is this: GameObject.FindGameObjectsWithTag

Once you have those objects, you’ll need to write a for loop to destroy them all.

EDIT: Oops… kevork beat me to it.

Got it kevork, I’ve only been coding for about 2,3 weeks( i love unity for this , ) so I still struggle a bit with syntext

So far I have this code ;
But it crashes unity when I use it

WARNING DO NOT USE

var i :int;

function OnMouseOver () 
{
 if (Input.GetMouseButtonDown(0))
 {
i=16;
  i--;
  while(i>1)
 {
 
 
 Destroy(gameObject.FindWithTag("enemy"));
			
			
 }
 }
 }

You never change the value of i in your while loop therefore it runs infinitely.

Use kevork’s code. Yours has an infinite loop, since i-- is outside the loop. It won’t work anyway even if fixed, since it will just destroy the first object it finds with the “enemy” tag 15 times (which isn’t any better than destroying it once :wink: ); it won’t destroy 15 different enemies. (That’s kind of an odd way to loop anyway; typically you’d use “for (var i = 0; i < 15; i++)”. You don’t need or want to declare i outside the function; get rid of that. Always use local variables where possible.)

–Eric

Fixed it ,

var i :int;
var numbergone : float = 3; 
var destroyus : String;
function OnMouseOver () 
{
 if (Input.GetMouseButtonDown(0))
 {

 
  while(i<numbergone)
 {
  i++;
  yield WaitForSeconds(0.0001);
  Destroy(gameObject.FindWithTag(destroyus));
			
			
 }
 }
 }

For some reason it ignores the number gone var , but anyway It does what i need—
Need to fix this, it will stop at the numbers gone var total, but as I never resets it feels like a one use button…

Now its perfect

var i :int;
var numbergone : float = 3; 
var destroyus : String;
function OnMouseOver () 
{
 if (Input.GetMouseButtonDown(0))
 {

 
  while(i<numbergone)
 {
  i++;
  yield WaitForSeconds(0.0001);
  Destroy(gameObject.FindWithTag(destroyus));
			
			
 }
     i=0;}
 }

You should probably change the “yield WaitForSeconds(0.0001);” to “yield;”

Thanks for the tip, but I haven’t seen a real problem with the script as is ,
Like your talking about .000010 seconds to get rid of 10 items

“yield WaitForSeconds(0.0001)” won’t actually yield for .0001 seconds. The least amount of time it can yield for is one frame, which will probably be around .01 - .02 seconds. So you might as well just use yield, which waits a frame. However, you should really really use kevork’s code, and not do it using yield. Even though it “works” in this situation, the code you posted is flawed in a number of ways and can easily break.

–Eric

I couldn’t get kevork’s code to compile .
This is at most a temporary work around until I find a way to destroy all the game objects with said tag at once .

It’s C#; the Unityscript equivalent would be

var gos = GameObject.FindGameObjectsWithTag(destroyus);
for (var go in gos)
     Destroy(go);

–Eric

thanks!

Although I will keep my own code around in case I need a separate function to only kill 2 enemies .

Might I suggest PoolManager? Especially if you’re planning on Destroy/Instantiating a lot of objects in your game. Be easier then to destroy (or despawn ) all enemies in a pool group.