How to next a cooldown sequence inside a for loop?

Hey guys,

I’m trying to have several enemies spawn at a “door” object every 5 seconds or so and then move toward the player.

But in addition to the five second delay between spawnings of the group, I also need to have a short 1/4 second cooldown between spawnings of the individual enemies. So, it would be five enemies spawn, about 1/4 second apart, then there is a five second delay, and then another five enemies spawn, and so on…

The problem I’m having is that I can’t figure out how to get that 1/4 second cooldown between individual enemies. I tried putting a coroutine inside my for loop, but that doesn’t seem to work. So I must be doing it wrong.

Does anyone know what the right way to do this would be?

Here is the script that I attached to the “door” object.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DoorScript : MonoBehaviour {

    public GameObject enemyPrefab;          // the enemy game object

    private float enemyRate = 5.00f;        // the number of enemies that spawn at each interval
    private float enemyDelay = 0.25f;       // the amount of time, in seconds, between enemies
    private float enemyInterval = 5.00f;    // the amount of time, in seconds, between spawnings
    private float nextEnemy = 5.00f;        // the amount of time left, in seconds, before the next spawning

	void Update ()
        nextEnemy -= Time.deltaTime;        // that time to the next spawning counts down
        Debug.Log("nextEnemy " + nextEnemy);

        if (nextEnemy <= 0)                 // when the countdown reaches zero
            nextEnemy = enemyInterval;      // the countdown between spawnings is reset

            for (int i = 1; i < enemyRate; i++)     // for each enemy to be spawned this interval, iterate through the for loop...
                Instantiate(enemyPrefab, transform.position, Quaternion.identity);     // instantiate enemy at the door at the default rotation


    public void MouseSpawnDelay()

    public IEnumerator MouseSpawnDelayCo()
        yield return new WaitForSeconds(enemyDelay);

Use two different timer floats (one for the quarter-second spawns, one for the longer wait time between waves).

Don’t use a for loop here --Update runs 60 times per second and you’re only spawning an enemy 4 times per second, so you don’t really want to iterate over anything, you just need to figure out whether it’s time to spawn an enemy or not.

To do this, use an integer to keep track of how many enemies are left in the wave. If the integer is zero, then the wave is on the five-second cooldown.

You can use:
InvokeRepeating(“functionName” , startTime , RepetionInterval)

Add any conditions or constraints or customize the time intervals according to your need.

@Commoble That worked! Thanks a million. I really had trouble wrapping my head around this one.

For anyone interested, here’s the code that I wrote that ended up working:

public GameObject enemyPrefab;  // the enemy game object

    public int enemyRate;         // the number of enemies that spawn at each interval

    public int enemiesLeftInWave;   // number of enemies left to spawn this wave
    public float enemyInterval;     // time in seconds between individual enemy spawnings
    public float waveInterval;      // time in seconds between enemy wave spawnings
    public float nextEnemy;         // time left in enemy countdown
    public float nextWave;          // time left in wave countdown

	void Start ()
        enemiesLeftInWave = enemyRate;  // enemiesLeftInWave is set
        nextEnemy = enemyInterval;            // nextEnemy is set
        nextWave = waveInterval;                  // nextWave is set
	void Update ()
        nextWave -= Time.deltaTime;         // countdown nextWave 
        Debug.Log("nextWave " + nextWave);

        // is it time to spawn a wave?
        if(nextWave <= 0)   
            nextEnemy -= Time.deltaTime;    // countdown nextEnemy 
            if (nextEnemy <= 0)              // is it time to spawn an enemy?
                Instantiate(enemyPrefab, transform.position, Quaternion.identity);     // spawn enemy at the door at the default rotation
                nextEnemy = enemyInterval;  // nextEnemy countdown is reset
                enemiesLeftInWave -= 1;     // countdown enemiesLeftInWave 
                if (enemiesLeftInWave == 0)
                    enemiesLeftInWave = enemyRate;  // enemiesLeftInWave is reset
                    nextWave = waveInterval;        // nextWave countdown is reset