Destroy all object with tag

Hi,

I’m new to unity and programming and i’m trying to make a simple game and I’m stuck.

I have a player object that collides with an enemy object, and both destroys, so far so good.

But the problem is that I want all of the spawned enemies to be destroyed if one of them collides with the player.

The enemy objects has a tag = enemy, and are prefab clones.

Here is my code that is attached to the player:

using UnityEngine;
using System.Collections;

public class DestroyByContact : MonoBehaviour
{
    public GameObject explosion;
    private GameController gameController;

   
    void Start ()
    {
        GameObject gameControllerObject = GameObject.FindWithTag ("GameController");
        if (gameControllerObject != null)
        {
            gameController = gameControllerObject.GetComponent <GameController>();
        }
        if (gameController == null)
        {
            Debug.Log ("Cannot find 'GameController' script");
        }
    }

   
    void OnTriggerEnter(Collider other)
    {
        if (other.tag == "Boundary")
        {
            return;
        }

   

        Instantiate(explosion, transform.position, transform.rotation);
        Instantiate(explosion, other.transform.position, other.transform.rotation);

        Destroy(other.gameObject); //Enemy that collided, but want all enemies destroyed.
        Destroy(gameObject); //Player


        gameController.GameOver ();

    }


}
GameObject[] enemies = GameObject.FindGameObjectsWithTag("Enemy");
foreach(GameObject enemy in enemies)
  GameObject.Destroy(enemy);
1 Like

@ThomasCreate 's solution would work!

Alternatively, and I think ideally, you should avoid having to “Find” GameObjects in a scene hierarchy. Instead, for any such purpose, you should hold references of such spawn-able entities somewhere in your code separately.

A way to go about your situation could be that

  1. You declare a static System.Action in your Enemy component attached to each enemy gameobject
  2. Send out an enemy’s own reference to the action in Start() method of your Enemy class
  3. Have A class, say GameLogic, contain the action definition for your enemies. So in this class, you could keep a list of all enemies
  4. On collision with an enemy, tell the GameLogic to destroy gameobjects associated with all enemies.

FindObjectsWithTag() is very fast though (much faster than regular GameObject.Find()), and he only has to call it once; it’s not like it’s done every frame. The extra bookkeeping required to manually keep a reference to all enemies will not pay off, and it only serves to make the code more complex.

I wouldn’t be surprised if the tag system basically is doing what you are suggesting, but built-in.

Thank you for your help :slight_smile:
I have only two more questions about my problem.

The code worked, but not all the objects were destroyed.
For example:
I have a hazard of 10 enemies moving towards the player.
Five enemies is visible in the scene when my player collide with one of them and all 5 destroys thanks to the new code, but the five that hasn’t yet arrived into the scene aren’t destroyed. I hope you understand what I mean :slight_smile:

Second question,
Is it possible to make all of the enemies explode with Instantiate like the player and the enemy that collided?

FindObjectsWithTag() is way faster than GameObject.Find(), agreed! It also keeps a list of all gameobjects with a specific tag. But generally, simple arrays are the fastest, followed by Lists and then FindObjectsWithTag().

@off112 … If the other 5 enemies spawn after the collision, they would obviously not get destroyed. The logic that destroyed them is already in the past. I hope you understand what I mean :wink:

I don’t really get your second question.

1 Like

OK, I think I understand.

I’ll hope I can explain my second question better. If you look at my code above, that I trigger an explosion with Instantiate when the player collide with an enemy. But it’s only the enemy that collides that will explode. I would like that all the enemies explodes.

Just instantiate explosions at the enemy positions in the foreach loop that destroys them. :wink:

  1. Place “Enemy” script on each enemy object.
  2. Write “OnDestroy” method in Enemy class and instantiate an explosion gameobject inside it at the current position. Something like this:
 public class Enemy : Monobehavior
{
        void OnDestroy()
        {
                Instantiate(Resources.Load("EXPLOSION_PREFAB_PATH"), transform.position, Quaternion.identity)
        }
}

I haven’t tested the code for any spelling mistakes, so maybe you should.

1 Like