I’m trying to make a wave spawners that summons enemies.
Once all enemies from that wave are killed, the next one should start.
No matter how hard I try, It’ll only go straight to the next wave once one has finished or once i’ve killed everything, the next wave doesnt even start.
Here is what i’ve got:
using System.Collections;
using UnityEngine;
using TMPro;
public class WaveSpawner : MonoBehaviour
{
int waveCount = 1;
public float spawnRate = 1.0f;
public float timeBetweenWaves = 3.0f;
public int enemyCount;
public GameObject enemy;
public bool waveIsDone = true;
public Transform SpawnPoint;
public GameObject[] EnemyRemains;
void Update()
{
EnemyRemains = GameObject.FindGameObjectsWithTag("Enemy");
if (waveIsDone == true)
{
StartCoroutine(waveSpawner());
}
}
IEnumerator waveSpawner()
{
waveIsDone = false;
for (int i = 0; i < enemyCount; i++)
{
GameObject enemyClone = Instantiate(enemy, SpawnPoint);
yield return new WaitForSeconds(spawnRate);
}
spawnRate -= 0.1f;
enemyCount += 3;
waveCount += 1;
if (EnemyRemains.Length == 0)
{
yield return new WaitForSeconds(timeBetweenWaves);
waveIsDone = true;
Debug.Log("ITWORKS");
}
}
The problem with your wave spawner is that you only check once at the end of the spawning co-routine if all remaining enemies were killed (directly after increasing the waveCount in waveSpawner().
If you would check the length of EnemyRemains in your update loop, you should see your expected spawning behaviour.
Like sparka_ha said, you have several issues in your code. First of all, I would highly recommend to not start a new coroutine each wave. Just keep the coroutine running. Something like this should work
// [ ... ]
public List<GameObject> EnemyRemains;
void Start()
{
StartCoroutine(waveSpawner());
}
IEnumerator waveSpawner()
{
while (true)
{
for (int i = 0; i < enemyCount; i++)
{
GameObject enemyClone = Instantiate(enemy, SpawnPoint);
EnemyRemains.Add(enemyClone);
yield return new WaitForSeconds(spawnRate);
}
spawnRate -= 0.1f;
enemyCount += 3;
waveCount ++;
// wait until all enemies have been destroyed.
while (EnemyRemains.Length > 0)
{
EnemyRemains.RemoveAll(o=>o==null || !o.activeSelf);
yield return null;
}
yield return new WaitForSeconds(timeBetweenWaves);
}
}
Note that I changed the “EnemyRemains” variable from an array to a List so we can actually add the created enemies to the list. Inside the waiting loop after everything has been spawned we just remove all elements which have been destroyed (or that have been deactivated in case you use a pool). So when the list is empty, all elements have been destroyed / defeated.
There are a few things that may be problematic. You subtact 0.1 from the “spawnrate” (which is actually a spawn delay, not a rate) but your spawn rate starts at 1 second. So after 10 waves your “spawn rate” would be 0. It’s more common to reduce such attributes with a percentage by multiplying the value by something like “0.98f” which would reduce the delay by 2% of the current value. So it gets faster and faster but only approaches a delay time of 0 at infinity.