Attackers Still Spawning

When the timer runs out and the enemy count is 0, I do get the End Level Now! prompt in the console, but the enemies will continue to spawn. The 3rd time it spawns after it is supposed to stop, enemies will only spawn from 4 of the 5 set spawners. I have tried removing and adding the enemyspawner script from my spawner gameobjects. Some of my naming is different than the video, but I have watched the video a couple times to verify that I didn’t miss anything. Please let me know if you need to see any of the other scrips. I have pasted my EnemySpawer and LevelController scripts.

EnemySpawner Script

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemySpawner : MonoBehaviour
{

    bool spawn = true;
    [SerializeField] float minSpawnDelay = 1f;
    [SerializeField] float maxSpawnDelay = 5f;
    [SerializeField] Attacker[] enemyPrefabArray;


    // Start is called before the first frame update
    IEnumerator Start()
    {
        while (spawn)
        {
            yield return new WaitForSeconds(Random.Range(minSpawnDelay, maxSpawnDelay));
            SpawnEnemy();
        }
    }

    public void StopSpawning()
    {
        spawn = false;
    }

    private void SpawnEnemy()
    {
        var enemyIndex = Random.Range(0, enemyPrefabArray.Length);
        Spawn(enemyPrefabArray[enemyIndex]);
    }

    private void Spawn (Attacker myAttacker)
    {
        Attacker newAttacker = Instantiate(myAttacker, transform.position, transform.rotation) as Attacker;
        newAttacker.transform.parent = transform;
    }
}

LevelController Script

public class LevelController : MonoBehaviour
{

    [SerializeField] int numberOfAttackers = 0; //To verify game sees 0 attackers
    bool levelTimerFinished = false;

    public void AttackerSpawned()
    {
        numberOfAttackers++;
    }

    public void AttackerKilled()
    {
        numberOfAttackers--;
        if(numberOfAttackers <=0 && levelTimerFinished)
        {
            Debug.Log("End Level NOW!");
        }
    }

    public void LevelTimerFinished()
    {
        levelTimerFinished = true;
        StopSpawners();
    }

    private void StopSpawners()
    {
        EnemySpawner[] spawnerArray = FindObjectsOfType<EnemySpawner>();
        foreach(EnemySpawner spawner in spawnerArray)
        {
            spawner.StopSpawning();
        }
    }

}

Hi,

Welcome to our community! :slight_smile:

What you describe sounds as if this is a bug which gets solved in lecture “Quick Bug Fix - Win Condition”. Please keep watching! Did that lecture help you fix the problem?


See also:

I watched the lecture and its not quite the same. The game does know that there are 0 attackers and the timer has run out. I get the “End Level NOW!” output in the console. The problem is that enemies will just continue to spawn endlessly even though spawn should be set to false under StopSpawning() in the EnemySpawner script.

Are there any error messages in your console? And have you already tried to add Debug.Logs to your code to see what is going on during runtime, for instance, in the foreach loop of the StopSpawners method?

Also add a Debug.Log inside the while-loop of the Start method in the EnemySpawner class to see whether the value of spawn gets set to false and whether the while loop gets terminated.

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms