How to destroy all gameobjects with a tag (loop doesn't work)
I would like to destroy all gameobjects with a tag instantly, but when I use a loop:
GameObject[] enemies = GameObject.FindGameObjectsWithTag("Enemy");
foreach(GameObject enemy in enemies)
GameObject.Destroy(enemy);
Assets/scripts/moveboxes.cs(186,33): error CS1503: Argument #1' cannot convert
UnityEngine.GameObject[]' expression to type `UnityEngine.Object'
Thanks
Code works fine for me in a test scene. Exactly which line is 186 that's giving the error? $$anonymous$$ake sure you aren't using 'enemies' or 'enemy' anywhere else in the method.
Answer by beau101023 · Dec 11, 2015 at 01:27 AM
This script tested fine for me in unity 5.1.2f1.
public class Kill_Enemies : MonoBehaviour {
void Start ()
{
GameObject[] enemies = GameObject.FindGameObjectsWithTag("Enemy");
foreach(GameObject enemy in enemies)
GameObject.Destroy(enemy);
}
}
Your answer
Follow this Question
Related Questions
How to carry over button sound to another scene 1 Answer
NullReferenceException: Error from the GetStyle() - Method 1 Answer
Rayscast from the GameObject that is not the player does not work 0 Answers
UnityAction Vs UnityEvent 1 Answer
Error `UnityEngine.Component' does not contain a definition 1 Answer