- Home /
WaveSpawn Script. Help
Can someone help me with this i think there is something wrong with the script. It is supposed to spawn 5 enemies in round 1, 10 in round 2, 15 in round 3 etc But when all the enemies in any round are dead (enemyCounter == 0) the script doesn't start spawning again.
please help.
var spawnPoints : Transform[];
var zombiePrefab : GameObject[];
var yeildTimeMin = 2;
var yeildTimeMax = 5;
static var enemyCounter = 0;
var spawnXOffsetMin = 0;
var spawnXOffsetMax = 0;
var spawnZOffsetMin = 0;
var spawnZOffsetMax = 0;
var defaultSpawnNum = 5;
var isSpawning = false;
var waveNum = 1;
function SpawnEnemies(wave : int)
{
var spawnNum = (defaultSpawnNum + 5 * (wave - 1));
isSpawning = true;
for(var i = 0; i < spawnNum; i++)
{
yield WaitForSeconds(Random.Range(yeildTimeMin, yeildTimeMax));
var object : GameObject = zombiePrefab[Random.Range(0, zombiePrefab.Length)];
var positions : Transform = spawnPoints[Random.Range(0, spawnPoints.Length)];
Instantiate(object, positions.position + Vector3(Random.Range(spawnXOffsetMin, spawnXOffsetMax), 0, Random.Range(spawnZOffsetMin, spawnZOffsetMax)), positions.rotation);
enemyCounter ++;
}
isSpawning = false;
}
function UpdateWave()
{
waveNum += 1;
gameObject.GetComponent(AI_Damage).maxHealth += 50;
SpawnEnemies(waveNum);
}
function Start ()
{
SpawnEnemies(waveNum);
}
function Update ()
{
if(enemyCounter == 0 && !isSpawning)
{
UpdateWave();
}
}
Did you try to print enemyCounter
variable in update before if()
statement? I don't see code which decreases enemyCounter, too.
oh ok. how do you think i should go about doing that? es i did and your right its not counting down. how do you think i should implement that? i was thinking about adding the enemy health and when that reaches 0 destroy the game object and then $$anonymous$$us the counter by 1?
That should work yes. Track the enemy health, then destroy the object once it's health is gone and reduce the enemyCounter by 1 as you said. Currently you don't have any code I can see actually decreasing that counter so it'll never hit the update function.
ok i've broken it all together! it doesn't seem to be working for me at all! any chance someone could just change the code so i can see where i'm going wrong...?
Your answer
Follow this Question
Related Questions
How to make the clones of a zombie continue to clone? 2 Answers
How would I create a script that spawns objects more frequently as time goes on? 3 Answers
Spawn waves & wait till it's killed, spawn again. 1 Answer
Character respawns invisable? 1 Answer
Spawning a new player instance (UNET) 3 Answers