using UnityEngine; using System.Collections; using System.Collections.Generic; public class WaveSpawner : MonoBehaviour { [System.Serializable] public class Wave { public GameObject enemyPrefab; // Enemy prefab to spawn public int count = 5; // Number of enemies in this wave public float spawnRate = 1f; // Enemies per second (1 = 1/sec) } [Header("Setup")] public Wave[] waves; public Transform spawnPoint; [Tooltip("Optional parent to keep the hierarchy tidy")] public Transform containerParent; public float timeBetweenWaves = 5f; public bool autoStart = false; public bool IsSpawning { get; private set; } public bool AllWavesDone { get; private set; } public readonly List spawned = new List(); void Start() { if (autoStart) StartSpawning(); } public void StartSpawning() { if (IsSpawning || AllWavesDone) return; StartCoroutine(SpawnRoutine()); } IEnumerator SpawnRoutine() { IsSpawning = true; AllWavesDone = false; for (int w = 0; w < waves.Length; w++) { var wave = waves[w]; for (int i = 0; i < wave.count; i++) { SpawnEnemy(wave.enemyPrefab); yield return new WaitForSeconds(1f / Mathf.Max(0.0001f, wave.spawnRate)); } if (w < waves.Length - 1) yield return new WaitForSeconds(timeBetweenWaves); } IsSpawning = false; AllWavesDone = true; } void SpawnEnemy(GameObject enemyPrefab) { if (enemyPrefab == null || spawnPoint == null) return; var go = Instantiate(enemyPrefab, spawnPoint.position, spawnPoint.rotation, containerParent); var eb = go.GetComponent(); if (eb != null) spawned.Add(eb); } }