Destroy all items with tag (string)

I have the following line in my code:


resetString is a string set before the function is called.

The errors I get are:

Assets/Quiz Minigame/_Scripts/QuizSystem.cs(390,25): error CS1502: The best overloaded method match for `UnityEngine.Object.Destroy(UnityEngine.Object)' has some invalid arguments


Assets/Quiz Minigame/_Scripts/QuizSystem.cs(390,25): error CS1503: Argument `#1' cannot convert `UnityEngine.GameObject[]' expression to type `UnityEngine.Object'

I probably missed something, but don’t know what :confused:

I tried storing the gameobjects in a temporary list and deleting each item in the list, but that causes more severe problems with how I set up my code. I don’t have much time to finish the project so I can’t afford rewriting everything at the moment.

Any ideas/suggestions?

@Crixu, the error you are getting there tells you that you are trying to feed the wrong type to the Destroy method. Indeed you are trying to feed an array of GameObjects, but Destroy only accepts one.

To efficiently destroy all the objects returned by FindGameObjectsWithTag I would write the following code:

GameObject[] toBeDestroyed = GameObject.FindGameObjectsWithTag(resetString);
for (int i = 0; int < toBeDestroyed.Length; i++)

toBeDestroyed = null;
Hope this helps :wink:
I forgot to add this: the code assumes that (1) the tag exists and that (2) the resetString is not empty. To make it error proof we should enclose the above code in a try/catch block to catch the UnityException raised in those two situations.

The problem is that your using Destroy (which takes a parameter of type Object) passing an array of game objects. Try this one:

GameObject[] objectsToDestroy = GameObject.FindObjectsWithTag(resetString);

for (int i = 0; i < objectsToDestroy.Length; i++)


I don’t think Destroy(GameObject.FindGameObjectsWithTag(resetString)); will work.

You’re trying to delete an array not a GameObject. The Destroy method takes a GameObject as parameter.
Also if you put them in a List and try to delete them using a loop, you’d still have problems. You could just disable them using

foreach(GameObject g in myList){ g.SetActive(false); }

You could tell each of them to destroy themselves by sending a message to each of them using something like this…

foreach(GameObject g in myList){ gameObject.SendMessage("Suicide", 2f); }

Then in a scirpt attached to each gameObject with the tag, you’ll have a method named Suicide like this:

void Suicide(float timeToDie){ Destroy(gameObject, timeToDie); }