Destroy All Objects with Tag

if(collision.gameObject.tag == "Nuke"){
 
		nukeTargets = GameObject.FindGameObjectsWithTag("Elf");
		
		Destroy(nukeTargets);
		
	}

This returns: InvalidCastException: Cannot cast from source type to destination type.
Goblin.OnCollisionEnter (UnityEngine.Collision collision) (at Assets/Standard Assets/Scripts/My Scripts/Goblin.js:57)

Basically, when I touch the “Nuke” object, I want to destroy all objects tagged “Elf”.

Help?

FindGameObjectsWithTag() returns an array.

Destroy() expects one object.

You can handle the difference using a loop, calling Destroy() once for each item in the array.

Here’s an example in C#:

foreach (var nukeTarget in nukeTargets) {
    Destroy(nukeTarget);
}

Here’s an example in JS:

for (var nukeTarget in nukeTargets) {
    Destroy(nukeTarget);
}

edit: fixed syntax per Eric5h5 below

The JS example should be the same as the C# example, except use for instead of foreach.

–Eric

Ah, right! I’m rusty with JS, but newcomers seem to prefer it. Fixed with an edit. Thanks.