3
\$\begingroup\$

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;
}

}
\$\endgroup\$
2
  • \$\begingroup\$ This looks very similar to your previous question. If you want to clarify/refine your earlier question, please use the edit link instead of posting a new question. If this is about a different topic, please edit it to clarify the difference. \$\endgroup\$
    – DMGregory
    Commented Jun 5, 2018 at 20:54
  • \$\begingroup\$ My previous question is about enemy waves. Here I don't ask about waves. here my problem with Instantiate enemy from list. Look at wave 2 part, you will see that enemy 0 and 1 from list come up to the game scene on second. what I want is to make every enemy have number of Instantiate and time between spawn. I hope i cleared my question to you. \$\endgroup\$
    – Omer
    Commented Jun 5, 2018 at 21:16

1 Answer 1

3
\$\begingroup\$

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.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .