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();
}
}
}