how to destroy all objects with tag "soandso"?

hello forum.

i want to destroy. No i just want to destroy a number of cloned Objects which were created on runtime with Instantiate. During the creation i tag these objects with “CurvePoint”.
I need to destroy those tagged clones by calling the function DestroyClones () during runtime. I tried to make it kinda like in the Script Reference under ‘GameObject.FindGameObjectsWithTag’ only with destroy. But i am missing something.

static function DestroyClones () {
	
	var clones = GameObject.FindGameObjectsWithTag ("CurvePoint"); 
	
	for (var i in clones) {
	
		//   this creates an error message
		Destroy (clones); 
		
		//   this prints 'hello' i times but destroys only one clone
		//print("hello");
		//var aClone = GameObject.FindWithTag ("CurvePoint");
		//Destroy (aClone);		
		
	}
}

basicly i want to destroy all objects with the tag “CurvePoint”.

What error message does it produce?

If it was C# I would say that it’s because you can’t destroy objects that you are iterating through using a foreach loop.

Try using a normal for loop.

for (var i=0; i < array.length; i++)
{ 
   Destroy(array[i]);
}

it is a JavaScript. the error on just Destroy (clones); is:

BCE0023: No appropriate version of ‘UnityEngine.Object.Destroy’ for the argument list ‘((UnityEngine.GameObject))’ was found.

thanx so far!
I try a normal for loop.

OK, your code should be

Destroy(i)

not

Destroy(clones)

shouldn’t it? You can’t destroy the object you are iterating through.

oh. Yes!
everything works perfect now.
Thank you very much! :smile: