Trying to add a delay to an Instantiate in a For Loop
Hello, i am pretty new in unity coding with C#.
I try to code a spawn manager with an index of different prefabs that make random wave with a for loop making more and more enemies each new wave.
It's working well dispite one thing : enemies often spawn at the same position and that make them collide and glitch and i would like to make them spawn (be instantiate) not all at the same time during a wave, make them spawn one by one after a short delay (0.5seconds for exemple). The goal is to have wave of 50 enemies, so its important to don't make them spawn at the same time.
I tried use IEnumerator and Coroutine with wait for seconds in many ways in the code but it never worked properly (no enemies spawns or i get inifinity of enemies)
How do i have to use WaitForSeconds properly for this code or maybe there is another way to do it ?
PS : ObjectsOfType is a script attached to all enemies that i use to count enemies in this script.
thank you in advance
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class SpawnManagerXaxisTest : MonoBehaviour
 {
     public GameObject[] enemyPrefabs;
     public float spawnRangeZ = 20;
     public float spawnPosX = 20;
 
     public int enemyCount;
     public int waveNumber = 1;
     private void Start()
     {
         SpawnRandomEnemy(waveNumber);
     }
     void SpawnRandomEnemy(int enemiesToSpawn)
     {
         int enemyIndex = Random.Range(0, enemyPrefabs.Length);
         for (int i = 0; i < enemiesToSpawn; i++)
         {
             Instantiate(enemyPrefabs[enemyIndex], GenerateSpawnPosition(),
             enemyPrefabs[enemyIndex].transform.rotation);
         }
     }
 
 
     void Update()
     {
         enemyCount = FindObjectsOfType<MoveMiddle>().Length;
         if (enemyCount == 0)
         {
             waveNumber++;
             SpawnRandomEnemy((waveNumber));
         }
     }
 
     private Vector3 GenerateSpawnPosition()
     {
         Vector3 spawnPos = new Vector3(spawnPosX, 1, Random.Range(-spawnRangeZ, spawnRangeZ));
         return spawnPos;
     }
 
 }
  
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                