- Home /
NEED HELP! Wave spawner just wont work!,NEED HELP! Wave spawner not working :(
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");
}
}
}
I am not sure but it is likely because of the position of the StartCoroutine command. You cannot use StartCoroutine normally in Update. You need to use call your IEumarator in Start function and use Start in Update.
For example: void Start() { StartCoroutine(waveSpawner()); }
void Update()
{
Start();
}
Note: It is not a full script but I think you can pace them to work.
im not too sure about this, coroutines can be called quite fine with Update directly plus i would advise against calling Start() from anywhere. I think the real problem is that waveIsDone is only set true if the coroutine is called which is called when it is true and there are no enemies, aka after any enemy is instantiated, it'll never be called...im in a bit of a hurry rn, so ill look into this when i wake up tomorrow hehe, please excuse me @jaydentajwilson
Answer by sparka_ha · Sep 06, 2021 at 05:25 PM
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.
Also I would recommend not to use 'GameObject.FindGameObjectsWithTag("Enemy");' in your Update loop since it usually is considered an operation that is too costly to do it every frame (which is the rate at which Update() is called).
Instead I would recommend using a more 'event-based' approach where killed enemies notify the WaveSpawner about their death.
If that is too complicated, you could also use a separate co-routine checking for the end of the wave that is called less frequent (twice a second or less frequent may be totally fine for your use case)
Answer by Bunny83 · Sep 07, 2021 at 10:40 AM
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.