- Home /
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
}
Answer by roojerry · Jan 03, 2014 at 05:19 PM
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
Answer by Kiloblargh · Jan 03, 2014 at 05:22 PM
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++;
}
}
Thank You. One question, does this make enemiesPerWave$$anonymous$$ax and enemiesPerWave$$anonymous$$in the wave number? how can I change that value. Why is waveNumber in the brackets? It would be good if the enemies gets multiplies by the wave or something along the lines of that.
If you don't know arrays, learn them- Arrays and Lists are key to doing anything useful at all in Unity. An int[]
is just several int variables in one variable. You can see in the inspector it has as size and that size is 0. You can set the size to 10 if you want ten waves, then all those slots will open up. 'waveNumber' is being used as the array index (the n
th item in each array.)
Suppose you want 4-6 enemies the first wave, 5-10 the second wave, and 12-18 the third wave. So you would fill in enemiesPerWave$$anonymous$$in like
4
5
12
...
and enemiesPerWave$$anonymous$$ax like
7
11
19
...
If you wanted unlimited waves, you could come up with a function to set the $$anonymous$$ and max, this was just an example to show how you could randomize within a pre set range for each wave.
Thanks $$anonymous$$iloblargh and and brianruggieri. :)
Awesome, I will have to review arrays, I think I've forgotten most of that stuff. It works well now that I know what to do. Do you know why I got this error?
IndexOutOfRangeException: Array index is out of range.
WaveStart+$CreateEnemies$65+$.$$anonymous$$oveNext () (at Assets/Scripts/WaveStart.js:27)
Yes, I do. Every time a wave ends, waveNumber goes up by 1. When waveNumber is greater than the number of entries in enemiesPerWave$$anonymous$$in or enemiesPerWave$$anonymous$$ax; it's going to crash. You need to catch that situation with if (waveNumber >= enemiesPerWave$$anonymous$$in.Length)
and then do something keep sending the maximum number of enemies or displaying a "Game over, you won!" message.
Your answer
Follow this Question
Related Questions
Randomize Spawn Times 0 Answers
How to create random spawn for an object? (C#) 0 Answers
WaveSpawn Script. Help 0 Answers
my wave spawner not working properly 1 Answer
Enemy Select 1 Answer