Start Coroutine works once but not a second time
So i'm trying to add a boss wave into the Space shooter game unity has on its Learn page. I'm trying to make it so that on wave 10 you spawn a boss and after the boss is dead you return to the regular waves. Start Co-routine works for them but not for mine and I have used it plenty of other times successfully.
private void Update() {
if (restart)
{
if (Input.GetKeyDown(KeyCode.R))
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
}
if (wave == 10)
{
StartCoroutine(BossWave());
}
if (Input.GetKeyDown(KeyCode.Q))
{
powerUp = true;
}
if (powerUp)
{
powerUpText.text = "Power Up : Press E";
if (Input.GetKeyDown(KeyCode.E))
{
playerController.fireRate = playerController.fireRate * 0.1f;
StartCoroutine(PowerUpWait());
powerUpLock = 1;
}
}
}
IEnumerator SpawnWaves () {
while (game == true)
{
yield return new WaitForSeconds(startWait);
while (round == true)
{
Debug.Log("round == true");
for (int i = 0; i < hazardCount; i++)
{
GameObject hazard = hazards[Random.Range(0, hazards.Length)];
Vector3 spawnPosition = new Vector3(Random.Range(-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
Quaternion spawnRotation = Quaternion.identity;
Instantiate(hazard, spawnPosition, spawnRotation);
yield return new WaitForSeconds(spawnWait);
}
yield return new WaitForSeconds(waveWait);
wave += 1;
testText.text = "Wave: " + wave;
if (gameOver)
{
restartText.text = "Press 'R' for Restart";
restart = true;
break;
}
}
}
}
IEnumerator BossWave ()
{
Vector3 spawnPosition = new Vector3(Random.Range(-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
Quaternion spawnRotation = Quaternion.identity;
Instantiate(boss, spawnPosition, spawnRotation);
round = false;
if (bossHealth.health <= 0)
{
round = true;
}
If you guys could throw me a bone that would be amazing! Thank you in advance for any advice you guys give me. You are awesome!
Answer by gashraf · Aug 05, 2018 at 01:20 PM
Hi @roger4325,
If I read your code correctly, the SpawnWaves keeps spawning new regular waves, except on wave#10 where the boss wave spawns the boss and turns the variable "round" to false. The variable "round" needs to be turned back to true once the boss is dead. Where I see an issue here is that you are not waiting for the boss to die... the if check in your BossWave routine is being checked only once, immediately after the birth of the boss... thereafter, you are exiting the coroutine, never to execute this check again! So here is what you can do, you can wait till the boss dies before exiting the coroutine, as follows:
IEnumerator BossWave()
{
...
round = false;
/* earlier code
if (bossHealth.health <= 0)
{
round = true;
}
*/
//new code
//keep waiting and yielding control till boss gets killed
while(bossHealth.health > 0)
yield return null;
//if you have reached here, means the boss is dead, yeah!
round = true;
}//end of coroutine
Your answer
Follow this Question
Related Questions
[solved] Collision damage and Health Controller dont work 2 Answers
Can't decompress package Unity 2017.1.f3 1 Answer
Hi, Unexpected symbol at end of file. 0 Answers
StartCoroutine error message in C# 1 Answer
ArgumentOutOfRangeException 0 Answers