using System.Collections.Generic; using UnityEngine; public class ZombieSpawnerLogic : MonoBehaviour { public GameObject Prefab; public byte CooldownSeconds = 5; public byte MaxSpawns = 5; private float _lastSpawn; private readonly List _spawns = new(); void Start() { _lastSpawn = Time.time; } void FixedUpdate() { RemoveDestroyedZombies(); AttemptZombieSpawn(); } private void AttemptZombieSpawn() { if (Time.time - _lastSpawn < CooldownSeconds) { return; } if (_spawns.Count >= MaxSpawns) { Debug.Log("Cannot spawn zombie, capacity reached"); return; } Debug.Log("Spawning zombie"); var newZombie = Instantiate(Prefab, transform); _spawns.Add(newZombie); _lastSpawn = Time.time; } private void RemoveDestroyedZombies() { _spawns.RemoveAll(zombie => zombie == null); } }