How to make waves of enemys spawn after few seconds.
Hi, I picked up unity less than week ago so I don't realy know what's wrong with this code. I've read that you can't use WaitForSeconds();in Update but I don't know why and I don't know how to do it otherwise. Can somebody help me how should I do it?
 public class Spawning : MonoBehaviour
 {
     public GameObject zombiePrefab;
     int randomSpawnZone;
     float randomPositionX, randomPositionY;
     Vector3 spawnPosition;
     public float enemySpawned = 5f;
     public float enemysToKill = 5;
     public float enemyAddedEveryRound = 1;
     
     void Start ()
     {
         for (int i = 0; i < enemySpawned; i++)
         {
             SpawnNewEnemy();
         }
     }
     
     void Update ()
     {
         EnemyWaveSpawner();
     }
     
     IEnumerator Wait()
     {
         yield return new WaitForSeconds(3);
     }
     
     void SpawnNewEnemy()
     {
         randomSpawnZone = Random.Range(0, 4);
 
         switch (randomSpawnZone)
         {
             case 0:
                 randomPositionX = Random.Range(-10f, 10f);
                 randomPositionY = Random.Range(-6f, -5f);
                 break;
             case 1:
                 randomPositionX = Random.Range(-10f, 10f);
                 randomPositionY = Random.Range(5f, 6f);
                 break;
             case 2:
                 randomPositionX = Random.Range(10f, 11f);
                 randomPositionY = Random.Range(-5f, 5f);
                 break;
             default:
                 randomPositionX = Random.Range(-11f, -10f);
                 randomPositionY = Random.Range(-5f, 5f);
                 break;
         }
         spawnPosition = new Vector3(randomPositionX, randomPositionY, 0f);
         Instantiate(zombiePrefab, spawnPosition, Quaternion.identity);
     }
     
     void EnemyWaveSpawner()
     {
         if (enemysToKill == 0)
         {
             Wait();
             enemySpawned += enemyAddedEveryRound;
             enemysToKill = enemySpawned;
             for (int i = 0; i < enemySpawned; i++)
             {
                 SpawnNewEnemy();
             }
         }
     }
     
 }
 
              If you want to execute a code-block over time like this, the thing you need is called Coroutine. You were partially on a right track there but the code didn't make much sense. Paczaj na to instead:
 public class Spawning : MonoBehaviour
 {
     [SerializeField] GameObject zombiePrefab;
     public int enemyAddedEveryRound = 5;
     public int enemiesSpawned = 0;
     
     void Start ()
     {
         StartCoroutine( EnemyWaveSpawnerRoutine() );
     }
     
     IEnumerator EnemyWaveSpawnerRoutine ()
     {
         SpawnWave();
 
         while( enemiesSpawned>0 )
         {
             SpawnWave();
             yield return new WaitForSeconds(3);
         }
     }
 
     void SpawnWave ()
     {
         for( int i=0 ; i<enemyAddedEveryRound ; i++ )
         {
             SpawnEnemy();
         }
         enemiesSpawned += enemyAddedEveryRound;
     }
     
     void SpawnEnemy ()
     {
         int randomSpawnZone = Random.Range(0, 4);
         float randomPosX;
         float randomPosY;
         switch( randomSpawnZone )
         {
             case 0:
                 randomPosX = Random.Range(-10f, 10f);
                 randomPosY = Random.Range(-6f, -5f);
                 break;
             case 1:
                 randomPosX = Random.Range(-10f, 10f);
                 randomPosY = Random.Range(5f, 6f);
                 break;
             case 2:
                 randomPosX = Random.Range(10f, 11f);
                 randomPosY = Random.Range(-5f, 5f);
                 break;
             default:
                 randomPosX = Random.Range(-11f, -10f);
                 randomPosY = Random.Range(-5f, 5f);
                 break;
         }
         Vector3 spawnPosition = new Vector3( randomPosX , randomPosY , 0f );
         Instantiate( zombiePrefab , spawnPosition , Quaternion.identity );
     }
 
 }
 
                 Answer by henkehedstrom · Jan 07 at 03:38 PM
You need to write StartCoroutine(COROUTINE()); So instead of calling the Wait function like Wait(); you have to write StartCoroutine(Wait()); 
You cannot write yield return new WaitForSeconds() in the update function becuse it returns void compared to your Wait function that returns an IEnumerator. I am not that knowledgeable about how coroutines actually work but here are some links if you want to learn more. https://docs.unity3d.com/ScriptReference/MonoBehaviour.StartCoroutine.html https://docs.unity3d.com/ScriptReference/Coroutine.html
Your answer