- Home /
Spawning enemies in waves
Hi,
I am trying to make a spawn system in waves. My spawn is working but the wave section is not.
Once one enemy dies, enemies are just randomly spawn out of control
var location:Transform;
var enemySelect:Rigidbody2D;
var spawn1:Transform;
var spawn2:Transform;
var spawn3:Transform;
var waveTime:float;
var waveLevel:int = 1;
var totalEnemy:int = 2;
var enemyAlive:int = 0;
var waitToWave:float = 10;
var waveMultiplier:int = 2;
var enemy1:Rigidbody2D;
var enemy2:Rigidbody2D;
var enemy3:Rigidbody2D;
function Awake ()
{
Spawn();
//enemyCount = GameObject.FindGameObjectsWithTag("Enemy");
//var tempobj : GameObject = GameObject.FindWithTag("Enemy");
var countScript : EnemyHealth = GetComponent(EnemyHealth);
}
function FixedUpdate () {
if (enemyAlive == 0) {
//the is no creature left; create a wave!
waveLevel++;
totalEnemy = totalEnemy * waveMultiplier;
Spawn();
}
if (EnemyHealth.health == 0){
enemyAlive--;
Debug.Log("enemy died");
}
}
if (waitToWave <= waveTime) {
waveTime = 0;
//reset time
//enemyAlive = totalEnemy;
}
function Spawn()
{
for(var i:int=0; i<totalEnemy; i++) {
enemyAlive++;
//select a random number, inside a maths function absolute command to ensure it is a whole number
var randomPick:int = Mathf.Abs(Random.Range(1,3));
//create a location 'Transform' type variable to store one of 3 possible locations declared at top of script
var location1:Transform;
location1 = spawn1;
var location2:Transform;
location2 = spawn2;
var location3:Transform;
location3 = spawn3;
if(randomPick == 1){
location = location1;
Debug.Log("Choose loc 1");
}
else if(randomPick == 2){
location = location2;
Debug.Log("Choose loc 2");
}
else if(randomPick == 3){
location = location3;
Debug.Log("Choose loc 3");
}
//select a random number, inside a maths function absolute command to ensure it is a whole number
var randomPickEnemy:int = Mathf.Abs(Random.Range(1,4));
//check what randomPick is, and select one of the 3 locations, based on that number
if(randomPickEnemy == 1){
enemySelect = enemy1;
Debug.Log("Choose enemy 1");
}
else if(randomPickEnemy == 2){
enemySelect = enemy2;
Debug.Log("Choose enemy 2");
}
else if(randomPickEnemy == 3){
enemySelect = enemy3;
Debug.Log("Choose enemy 3");
}
//create the object at point of the location variable
var thingToMake:Rigidbody2D;
Debug.Log(location);
thingToMake = Instantiate(enemySelect, location.position, location.rotation);
thingToMake.AddForce(Vector3(0,0,100));
//halt script for 1 second before returning to the start of the process
yield WaitForSeconds(5);
}
}
I have tried for hours trying to move loops around,brackets etc. but all has failed
what am i doing wrong?
Answer by LeGrandBlond · Apr 15, 2014 at 08:57 PM
I am afraid you are doing it in the wrong way. Calling WaitForSeconds() inside Spawn() which is called inside FixedUpdate() is incorrect. You need a co-routine. I suggest to have a look at the code here : http://unity3d.com/learn/tutorials/projects/space-shooter/spawning-waves Feel free to watch the video, it is an absolutely brilliant tutorial explaining how to spawn enemy waves!
i tried to play the video it has no sounds and why do you say i am doing it all wrong why exactly is it wrong ? it does work the only issue is when enemy is killed and the enemyAlive count drops to 0 it continuously spawns players when i look at debug it reports that enemy has died several times even doe it has not
It seems you want your code "fixed" here, rather than trying to learn what is causing it to fail.
As LeGrandBlond said, you approach to the problem is incorrect. Calling WaitForSeconds(5) at the end of your spawn function doesnt accomplish anything, it simply tells unity to wait 5 seconds before cleaning up Spawn function's information from memory.
It is not that case that the spawn function can only be run when another spawn function finishes. Your script is continuously running the spawn function and each of them is waiting for 5 seconds at the end(acomplishing nothing). What you need is a Coroutine to halt the CALLING of the spawn function.
However, I have found that InvokeRepeating can be easier to understand at first. Take a look at the Unity Documentation for InvokeRepeating and StartCoroutine
Your answer
![](https://koobas.hobune.stream/wayback/20220613142809im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to spawn objects in a specific range of random location 1 Answer
instantiate a set amount 3 Answers
Randomize Spawn Times 0 Answers
Semi - Advanced Spawn System 1 Answer