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