Instantiate object with spawn time?

Instantiate object with spawn time?

I have script that makes waves of enemies. It selects enemy from array randomly. I want to add function allow me to select any object in my array and instantiating it for number of times. In wave 1, randomly select enemy from enemy list and represent for several of time. In wave 2, I want to select from my enemy list, and how many will instantiate, and time between spawn.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class wer : MonoBehaviour
{
public Wave[] waves;
public GameObject[] enemy;   // enemy list
private int enemies;
Wave currentWave;
int currentWaveNumber;
int enemiesRemainingToSpawn;
int enemiesRemainingAlive;
float nextSpawnTime;
public Transform[] spawnPoints;

void Start()
{
    NextWave();
}

void Update()
{

    if (enemiesRemainingToSpawn <= 0)
    {
        NextWave();
    }

    if (enemiesRemainingToSpawn > 0 && Time.time > nextSpawnTime)
    {

        if (currentWaveNumber == 1) // WAVE 1
        {
            enemiesRemainingToSpawn--;
            nextSpawnTime = Time.time + 1; // spawn time 1 second
            int spawnPointIndex = Random.Range(0, spawnPoints.Length);
            enemies = Random.Range(0, 1);
            Instantiate(enemy[enemies], spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);

        }

        if (currentWaveNumber == 2) // WAVE 2
        {
            for (int i = 0; i < enemy.Length; i++)
            {
                enemiesRemainingToSpawn--;
                int spawnPointIndex = Random.Range(0, spawnPoints.Length);
                Instantiate(enemy[0], spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);
                Instantiate(enemy[1], spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);

                // enemy [0] = Instantiate 5 times, time between spawn 1 second;
                // enemy [1] = Instantiate 7 times, time between spawn 1.5f second;

            }

        }

    }


} // end update

void NextWave()
{
    currentWaveNumber++;
    print("Wave: " + currentWaveNumber);
    if (currentWaveNumber - 1 < waves.Length)
    {
        currentWave = waves[currentWaveNumber - 1];
        enemiesRemainingToSpawn = currentWave.enemyCount;
        enemiesRemainingAlive = enemiesRemainingToSpawn;
    }

}

[System.Serializable]
public class Wave
{
    public int enemyCount; // Quantity or number of enemy to Instantiate
    public float timeBetweenSpawns;
}

}

Solutions/Answers:

Answer 1:

I would suggest creating a coroutine which takes three arguments, one for the gameobject which is to be instantiated, another for the number of times it should be instantiated, and the final parameter should be the amount of time between spawns.

Psuedocode

public IEnumerator InstantiateRandomCoroutine(Gameobject obj, int repeat, float cooldown) 
{ 
    for i < repeat {
        instantiate obj;
        yeild return WaitForSeconds(cooldown);
    }
}

I would suggest getting a random index value within the bounds of the array and using that to reference the GameObject being used as an argument to the coroutine. If you need more information on using coroutines then I would suggest looking reading this.

There may be some fundamental things you will have to change about your implementation but I would very much suggest reading about coroutines if you are looking to instantiate game objects repeatedly with a varying or constant pause in between.

For information about coroutines with multiple arguments please reference this discussion on Unity Answers. Credit to JinxM.

References