Is there a better way to tell when enemies are dead?

void OnTriggerStay2D(Collider2D other) {
if (other.gameObject.tag == “Antagonist” || other.gameObject.tag == “Skeleton” || other.gameObject.tag == “Wizard”)
{
AllEnemiesKilled = false;
}
else
{
AllEnemiesKilled = true;
}
}

I’m assuming I could use OnDestroy() and add an int for every enemy death and when it equals the number of enemies set the bool to true but I was kind of hoping to use this incase I want to randomize spawning later…

the simplest way to keep track of object instances is with a static variable.

class Enemy
{
    public static int numberOfEnimies;

    //constructor    
    Enemy()
    {
        numberOfEnemies++;
    }

    //deconstructor
    ~Enemy()
    {
        numberOfEnemies--;
    }
}

Then you get the number of enemies anytime by calling Enemy.numberOfEnemies you use the class name not the instance name because it is a static variable. Static variables are shared amongst instances of a class. You can also do Enemy enemy1; enemy1.numberOfEnemies; you can access it through the instance and no matter what instance it’s called from it will give the same amount. Here is how you do it with MonoBehaviours:

    class Enemy : MonoBehaviour
    {
        public static int numberOfEnimies;
    
        Start()
        {
            numberOfEnemies++;
        }
    
        OnDestroy()
        {
            numberOfEnemies--;
        }
    }

If you are instantiating and destroying your objects, if you are enabling and disabling them than you do it like this instead:

    class Enemy : MonoBehaviour
    {
        public static int numberOfEnimies;
    
        OnEnable()
        {
            numberOfEnemies++;
        }
    
        OnDisable()
        {
            numberOfEnemies--;
        }
    }

Or you can have all your enemies in a list and use list.Count:

 List<Enemy> enemies;
 enemies.Count;

I think the best way to go would be adding an int for each time an enemy is spawned and subtracting each time an enemy dies. Would you care to elaborate on why randomized spawning wouldn’t work with this method?