How to spawn multiple prefabs at different spawn rates
I consider myself to be pretty beginner level when it comes to Unity and programing in C#. In the project I'm working on my goal for this script is to have it be able to spawn different type of enemies at different times for each spawn point. Basically to create a single script for all spawn points instead of having to create a separate script for each enemy spawn point. This is what I have so far.
public class EnemyWaveSpawner : MonoBehaviour
{
public GameObject[] enemiesToSpawn;
//public float spawnRate;
//public float spawnStart;
private float waitToSpawn;
//public int[] waveStartNumber;
private int currentWaveNumber;
//private int waveCounter = 3;
public WaveManager waves;
// Start is called before the first frame update
void Start()
{
waitToSpawn = 0f;
}
// Update is called once per frame
void Update()
{
//SpawnManager();
SpawnEnemy(waves.waveStartNumber[0], waves.enemySpawnNumber[0], waves.spawnRates[0]);
currentWaveNumber = KaijuLevelManager.instance.waveNumber;
waitToSpawn -= Time.deltaTime;
if (waitToSpawn <= 0)
{
waitToSpawn = 0;
}
}
/*private void SpawnManager()
{
switch (waveCounter)
{
case 3:
SpawnEnemy(waves.waveStartNumber[0], waves.enemySpawnNumber[0], waves.spawnRates[0]);
break;
}
}*/
private void SpawnEnemy(int waveStart, int thisSpawn, float spawnRate)
{
if (KaijuLevelManager.instance.enemiesActive && currentWaveNumber >= waveStart)
{
if (waitToSpawn <= 0)
{
GameObject enemyClone = Instantiate(enemiesToSpawn[thisSpawn], transform.position, transform.rotation);
waitToSpawn = spawnRate;
}
}
}
}
This script is still a work of progress. I figured out how to set it up so I can spawn different enemies of my choice and for each start at different waves. The problem I have is that I can't really figure out how to spawn them at different times. As you could probably tell in my script there's a single spawn timer counting down. I can already see how this wont work for different spawn rates for each enemy. My first solution was to try put "waitToSpawn" local to the the function instead of the script like this.
private void SpawnEnemy(int waveStart, int thisSpawn, float spawnRate)
{
float thisWaitToSpawn = spawnRate;
if (KaijuLevelManager.instance.enemiesActive && currentWaveNumber >= waveStart)
{
thisWaitToSpawn -= Time.deltaTime;
if (thisWaitToSpawn <= 0)
{
GameObject enemyClone = Instantiate(enemiesToSpawn[thisSpawn], transform.position, transform.rotation);
thisWaitToSpawn = spawnRate;
}
}
}
The problem I run into when trying to do this is that "thisWaitToSpawn" will not reset itself to equal "spawnRate". If I try to put "thisWaitToSpawn" at zero from the beginning then it'll continuously spawn the object non stop(it will eventually stop when the level manager stops its countdown and turn the bool, enemiesActive, false). At this point I'm pretty much banging my head on the wall trying to figure this out. Do you have any possible suggestions on how to precede forward?
Your answer
Follow this Question
Related Questions
Make a gameobject spawn before enemies spawn? (C#) 1 Answer
Spawn system that doesn't instantiated enemies on top of player or each other (C#) 2 Answers
Instantiate wrong position 0 Answers
problem when i spawn to many Enemy the game be slower 1 Answer
Make a collision check when spawning with Instantiate 1 Answer