- Home /
Enemy Wave
Hey all, I'm new to Arrays and I've got my script spawning 1 enemy every wave but I wanted to make it so that the script will spawn +1 enemy every new wave (Wave 1 = 1, Wave 2 = 2, etc.) but I'm not sure how. Would someone mind making my script do that and explaining what they did so I can use it again in the future?
var textColor = Color.yellow;
var NumberEnemies : int;
var WaveNumber = 0;
var NewWave : boolean = false;
var SpawnPoints : Transform[];
var Enemies : Transform[];
function Awake () {
guiText.material.color = textColor;
}
function Update(){
NumberEnemies = ((GameObject.FindGameObjectsWithTag("Enemy").Length) - 1);
guiText.text = "Wave: " +WaveNumber;
if(NumberEnemies == 0 && !NewWave){
NewWave = true;
WaveNumber += 1;
SpawnEnemies();
}
}
function SpawnEnemies(){
SpawnEnemy = Instantiate(Enemies[0],SpawnPoints[0].position,SpawnPoints[0].rotation);
NewWave = false;
}
Answer by Dreamish · Aug 21, 2011 at 04:42 AM
Just add a for loop around the spawning code:
Instead of just
SpawnEnemy = Instantiate(Enemies[0],SpawnPoints[0].position,SpawnPoints[0].rotation);
You can write
for (var i = 0; i < WaveNumber; i++) {
SpawnEnemy = Instantiate(Enemies[0],SpawnPoints[0].position,SpawnPoints[0].rotation);
}
This will loop that piece of code as many times as the wave you're on. Since you've also put your spawn locations and enemies in arrays, you can change the indexes to for example Enemies[i], using the iterator for the loop (the counting variable) to spawn the enemy in location 0 of your array in the first wave, the enemies at locations 0 and 1 at the second, locations 0, 1 and 2 for the thrird wave and so forth, since the foor loop starts counting the i at 0 and continues up as long as it's smaller than WaveNumber. For this you need to make sure to have as many added enemies in your array as waves though. =)
Good luck!
Is there a way to increase the number of enemies after each wave because i could really use that. just curious.
Your answer
Follow this Question
Related Questions
Can anybody help me fix this wave spawning script please? 1 Answer
Spawn "n" Enemies Every "nth" Wave? 1 Answer
Way Point Assignment 4 Answers
Play Audio in sequence from children objects 3 Answers
Array problem,different result 1 Answer