- Home /
Question by
hagas12 · Jul 28, 2016 at 10:05 AM ·
coroutinetutorialspace shooterenemies
Checking if miniboss is destroyed to spawn enemies again
Hi all, I am expanding upon the space shooter tutorial by adding my own mini-boss. I want to start another wave of enemies through the SpawnWaves function after the miniboss is destroyed. How do I go about checking if the miniboss is destroyed and restarting the Game Controller's Coroutine so that the SpawnWaves() is reactivated?
Here is the code of my GameController:
using UnityEngine; using System.Collections;
public class GameController : MonoBehaviour {
public GameObject[] hazards;
public GameObject shrimpyBoss;
public Vector3 spawnValues;
public int hazardCount;
public float spawnWait;
public float startWait;
public float waveWait;
public float bossWait;
public float countSpawn;
public float afterMini;
public GUIText scoreText;
public GUIText restartText;
public GUIText gameOverText;
public bool gameOver;
private bool restart;
private int score;
public void Start (){
gameOver = false;
restart = false;
restartText.text = "";
gameOverText.text = "";
score = 0;
UpdateScore ();
StartCoroutine (SpawnWaves ());
}
void Update(){
if (restart) {
if (Input.GetKeyDown (KeyCode.R)) {
Application.LoadLevel (Application.loadedLevel);
}
}
}
public IEnumerator SpawnWaves ()
{
yield return new WaitForSeconds(startWait);
while(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);
countSpawn += 1;
//Starting Mini-Boss Mode after 50 spawns
if (countSpawn == 10) {
yield return new WaitForSeconds (bossWait); //boss starts after 5 seconds
spawnShrimpy (); //spawn MiniBoss
} else {
yield return new WaitForSeconds (spawnWait);
}
}
yield return new WaitForSeconds (waveWait);
if (gameOver) {
restartText.text = "Press 'R' for Restart";
restart = true;
break;
}
}
}
public void AddScore (int newScoreValue){
score += newScoreValue;
UpdateScore ();
}
//Mini-Boss event
public void spawnShrimpy(){
StopAllCoroutines ();
Vector3 spawnPosition = new Vector3 (0,0,10);
Quaternion spawnRotation = Quaternion.identity;
Instantiate (shrimpyBoss, spawnPosition, spawnRotation);
}
void UpdateScore(){
scoreText.text = "Score: " + score;
}
public void GameOver ()
{
gameOverText.text = "Game Over";
gameOver = true;
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612085650im_/https://answers.unity.com/themes/thub/images/avi.jpg)