Function to destroy all objects with tag

I’m trying to make a function that should destroy all objects with tag once.

Here is my script (It’s not working, unity just crashed).

public void DestroyAllEnemy()
    {
        while(GameObject.FindGameObjectsWithTag("Enemy")!= null)
        {
            Destroy(GameObject.FindWithTag("Enemy"));
        }
        return;
    }

You’ll want to store the results in a collection, then loop through and destroy each one.

Don’t use while loops. Only if you’re certain that it’ll be broken at some point.

use the method FindGameObjectsWithTag and store it into a variable.
Then use a reversed for loop to cycle through the objects and call Destroy.