title: Enemy wave spawner in unity
body: <p>I'm trying to create a wave spawner for a top down game that I'm creating. I have created the wave spawner script but when I hit play, nothing happens. The countdown doesn't begin. Ideally it should start from 2, once it reaches 0, the first wave should spawn with one enemy. Once that enemy is killed, the countdown should begin from 5 and once 0 is reached, the next wave with 2 enemies should begin and so on. The new wave should not begin until all the current enemies are destroyed.</p>
  
  <pre><code>public enum SpawnState { SPAWNING, WAITING, COUNTING };
  
  public SpawnState state = SpawnState.COUNTING;
  
  public Transform enemy;
  
  public float timeBetweenWaves = 5f;
  public float countDown = 2f;
  
  private int waveIndex = 0;
  
  public float searchCountdown = 1f;   
  
  void Update()
  {        
    if (state == SpawnState.WAITING)
    {           
      if (!EnemyisAlive())
         {
            WaveCompleted();
          }
          else
          {                
              return;
          }
      }
  
    if (countDown &lt;= 0f)
    {          
        if (state != SpawnState.SPAWNING)
        {              
            StartCoroutine(SpawnWave());
            countDown = timeBetweenWaves;
        }
        else
        {              
            countDown -= Time.deltaTime;
        }
  
    }
  
  }
  
  void WaveCompleted()
  {        
      state = SpawnState.COUNTING;
      countDown = timeBetweenWaves;
      SpawnWave();        
  }
  
  bool EnemyisAlive()
  {        
      searchCountdown -= Time.deltaTime;
      if (searchCountdown &lt;= 0)
      {
          searchCountdown = 1f;
          if (GameObject.FindGameObjectsWithTag("Enemy").Length == 0)
          {
              return false;
          }
      }
      return true;
   }
  
  IEnumerator SpawnWave()
   {        
      state = SpawnState.SPAWNING;
      waveIndex++;
      for (int i = 0; i &lt; waveIndex; i++)
      {
          SpawnEnemy();
          yield return new WaitForSeconds(0.5f);
  
      }
      state = SpawnState.WAITING;
      yield break;
   }
  
  void SpawnEnemy()
   {
      Instantiate(enemy, transform.position, transform.rotation);
   }   
  </code></pre>