- Home /
Can anybody help me fix this wave spawning script please?
I have this script here for spawning enemies in waves but when you play it, it only lays the number of enemies you enter and then stops, it doesn't start a new wave after that, so if anybody could please fix that and add anything necessary of completing the script so it can project waves of enemies, that would be GREAT. Thankyou.
Here is the script ... #pragma strict
var spawnPoints : Transform[];
var enemyPrefabs : GameObject[];
var yieldTimeMin = 2;
var yieldTimeMax = 5;
static var enemyCounter = 0;
var spawnXOffsetMin = 0;
var spawnXOffsetMax = 0;
var spawnZOffsetMin = 0;
var spawnZOffsetMax = 0;
var defaultSpawnNumber = 5;
var waveNumber = 1;
var isSpawning = false;
function SpawnEnemies(wave : int)
{
var spawnNum = (defaultSpawnNumber + 5 * (wave - 1));
isSpawning = true;
for(var i = 0; i < spawnNum;i++)
{
yield WaitForSeconds(Random.Range(yieldTimeMin, yieldTimeMax));
var object : GameObject = enemyPrefabs[Random.Range(0, enemyPrefabs.Length)];
var position : Transform = spawnPoints[Random.Range(0, spawnPoints.Length)];
Instantiate(object, position.position +
Vector3(Random.Range(spawnXOffsetMin, spawnXOffsetMax), 0,
Random.Range(spawnZOffsetMin, spawnZOffsetMax)), position.rotation);
enemyCounter++;
}
isSpawning = false;
}
function UpdateWave()
{
waveNumber++;
SpawnEnemies(waveNumber);
}
function Start ()
{
SpawnEnemies(waveNumber);
}
function Update ()
{
if(enemyCounter == 0 && !isSpawning)
{
UpdateWave();
}
}
Doesn't add new wave when? What shoud happen so it starts making new wave? From the script i understand that you have to kill all enemies and then the script is adding new wave. Does you'r enemies (That you are spawning with the wave) reduce the enemyCounter when they get killed?
It might be helpful to be able to see your enemy class script.
it dosent add a new wave when you kill all the zombies from the from the first wave, it just stops and your gametime is over.
And where do you tell the script that all the zombies have been killed (I.e. EnemyCounter = 0)?
Well, that's what I am trying to figure out, I am not a great at scripting or coding and I am still learning so I would love to take as much help as possible to figure out my problems I have.
Your answer
![](https://koobas.hobune.stream/wayback/20220613125812im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Spawn "n" Enemies Every "nth" Wave? 1 Answer
Need help with enemy spawner 2 Answers
Script issues [js] 0 Answers
Particle effect spawn help 1 Answer
Enemy Wave 1 Answer