Search Unity

Instantiating prefabs with coroutine delay not working

Discussion in 'Scripting' started by SlicksAndNitro, Feb 17, 2019.

  1. SlicksAndNitro

    SlicksAndNitro

    Joined:
    Jul 6, 2018
    Posts:
    2
    Greetings - Currently, I have an issue where I am attempting to instantiate a prefab multiple times, with a delay between each instantiation implemented through a coroutine. The issue is that the instantiating is happening all at once, ignoring the delays implemented in the coroutine.

    This is for a simple 2D space shooter where enemy waves are being spawned. Currently, it's attempting to spawn only one wave of 10 enemies, with a delay of 5 seconds between each enemy spawn.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class GameManager : MonoBehaviour {
    6.     // Object references
    7.     [SerializeField] GameObject enemyPrefab;
    8.  
    9.     // Game parameters
    10.     [SerializeField] float enemyStartingXPosition = 7f;
    11.     [SerializeField] float enemyStartingYPosition = 7f;
    12.     [SerializeField] int enemiesPerWave = 10;
    13.  
    14.     /// <summary>
    15.     /// Start is called on the frame when a script is enabled just before
    16.     /// any of the Update methods is called the first time.
    17.     /// </summary>
    18.     void Start() {
    19.         SpawnEnemyWave();
    20.     }
    21.  
    22.     void SpawnEnemyWave() {
    23.         for(int i = 0; i < enemiesPerWave; i++) {
    24.             StartCoroutine(SpawnEnemy());
    25.         }
    26.     }
    27.  
    28.     IEnumerator SpawnEnemy() {
    29.         Instantiate(enemyPrefab, new Vector3(enemyStartingXPosition + Random.Range(2f, 5f), enemyStartingYPosition + Random.Range(2f, 5f), 0), Quaternion.identity);
    30.         yield return new WaitForSeconds(5);
    31.     }
    32. }
    33.  
    If I run the above, all 10 enemies appear on my screen right at game start, whereas I was expecting each to appear after 5 seconds due to the delay in the coroutine.

    Rather than a for loop, I've tried wrapping the Instantiate() statement in an if loop checking for a left-mouse button click in the Update function. This enables enemies to be spawned "on-demand" and works fine. It's only when I call Instantiate() 10x in a for loop and change to a coroutine that the delay is not happening.

    Any help or insights would greatly be appreciated.
     
  2. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    .You are starting multiple coroutines which are all running at the same time. Put your for loop in the coroutine and start it once.
     
  3. SlicksAndNitro

    SlicksAndNitro

    Joined:
    Jul 6, 2018
    Posts:
    2
    Perfect! Thanks for the tip, I had tried multiple things but think I misunderstood how a coroutine truly performs. I managed to fix it per your advice.