[Bugfix] Attackers still spawning after "End Level Now!" prompt

I noticed that even after the timer ran out and the enemy count reached 0, I was still getting new attackers being spawned.

This resulted in the “End Level Now” prompt repeating a few times before the level actually ended.

I figured out that even though spawn was set to false, it doesn’t actually interrupt the coroutine.
So it actually executes one last SpawnAttacker() after it finishes WaitForSeconds().

If you’re also encountering this problem, I managed to fix it by adding an additional if statement before SpawnAttacker():

IEnumerator Start()
{
    while (spawn)
    {
        yield return new WaitForSeconds(Random.Range(minSpawnDelay, maxSpawnDelay));
        if (spawn) { SpawnAttacker(); }
    }    
}

Hope this will be of help to someone out there!

1 Like

Privacy & Terms