Question by 
               Higgsboson728 · Feb 14, 2017 at 03:44 AM · 
                c#3dwave  
              
 
              Advanced Wave Spawner
Hi, I made a simple wave spawner that spawns one type of enemies:
 void Update () {
         
         if (player.transform.position.y < -50) {
             
             waveIndex = 0;
             countdown = 15f;
         }
 
         if (enemiesAlive > 0) {
 
             return;
         }
 
         if (countdown <= 0f) {
 
             StartCoroutine (spawnWave());
             countdown = deltaWaves;
 
             return;
         }
 
 
         countdown -= Time.deltaTime;
         countdown = Mathf.Clamp (countdown, 0f, Mathf.Infinity);
 
     }
 
     IEnumerator spawnWave() {
 
         waveIndex++;
 
         for (int i = 0; i < waveIndex; i++) {
 
             spawnEnemy ();
             yield return new WaitForSeconds (0.5f);
         }
 
     }
 
     void spawnEnemy() {
 
         spawnIndex = Random.Range (0, Spawnpoints.points.Length - 1);
 
         Instantiate (enemy, Spawnpoints.points[spawnIndex].transform.position, Quaternion.identity);
 
         enemiesAlive++;
     }
 }
Basically, I want to edit the code above to make it support multiple enemy types in a single wave. How should I do this?
Thanks,
~Higgsboson728
(Please do not link me to the wiki)
               Comment
              
 
               
               
               Best Answer 
              
 
              Answer by UnityCoach · Feb 14, 2017 at 12:23 PM
First, you want the spawnEnemy method to be passed an enemy parameter :
 void spawnEnemy (GameObject enemy)
 {
     spawnIndex = Random.Range (0, Spawnpoints.points.Length - 1);
     Instantiate (enemy, Spawnpoints.points[spawnIndex].transform.position, Quaternion.identity);
     enemiesAlive++;
 }
Then, you want the spawnWave method to tap into a table of enemies with some cycling or random indexing.
 public List<GameObject> enemies; // a list of game objects that'll store the enemies references
 IEnumerator spawnWave()
 {
     waveIndex++;
     int index;
     for (int i = 0; i < waveIndex; i++)
     {
         index = Random.Range (0, enemies.Count); // using random here
         //index = (index >= enemies.Count-1) ? 0 : index+1; // or using cycling
         spawnEnemy (enemies[index]);
         yield return new WaitForSeconds (0.5f);
     }
 }
You could make a Wave class, like :
 [System.Serializable]
 public class Wave
 {
     public List<GameObject> enemies;
 }
Then, you could make the spawnWave method take a Wave for argument, like :
 IEnumerator spawnWave (Wave wave)
  {
      waveIndex++;
      int index;
      for (int i = 0; i < waveIndex; i++)
      {
          index = (index >= wave.enemies.Count-1) ? 0 : index+1;
          spawnEnemy (wave.enemies[index]);
          yield return new WaitForSeconds (0.5f);
      }
  }
Then you could add a List of waves to your spawner and iterate through it:
 public List<Wave> waves;
 StartCoroutine (spawnWave (waves [waveIndex]));
I'm sorry for being a noob, but how do I actually add the waves? Do I add them in a list?
Your answer
 
 
             