# How can I detect the number of enemies?

I’m making a game with waves of enemies. When an enemy dies it sends a message to an empty GameObject saying that it died to keep track of how many enemies are left. How can I determine how many enemies there are (I hoping to do some kind of semi-random spawning).

``````#pragma strict

var EnemyCount : ????;
var waveNumber = 1;

function Died ()
{
EnemyCount -= 1;
if(EnemyCount == 0)
{
waveNumber += 1;
CreateEnemies();
}
}

function CreateEnemies ()
{
//NotDone
}
``````

function CreateEnemies ()
{
EnemyCount = Random.Range(10,20);

``````    for(var i=0; i < EnemyCount; i++)
{
Instantiate(enemyPrefab);
}
}
``````

This should be able to get you started. Its up to you to decide how many enemies each wave will have. In this example I just pick a random number between 10 and 20

You were almost there- this should do it:

``````#pragma strict

var enemyCount : int;
var waveNumber : int = 1;
var enemiesPerWaveMin : int[]; //at least this many per wave (set up in inspector)
var enemiesPerWaveMax : int[]; //no more than this many per wave

function Died ()  {
enemyCount--;
if (enemyCount == 0) {
waveNumber++;
CreateEnemies();
}
}

function CreateEnemies ()  {
var howMany : int = Random.Range (enemiesPerWaveMin [waveNumber], enemiesPerWaveMax [waveNumber]);
for (var e : int = 0; e < howMany; e++)  {

//spawn one enemy

enemyCount++;
}
}
``````