using System.Collections; using System.Collections.Generic; using UnityEngine; public class SpawnManager : MonoBehaviour { [SerializeField] public GameObject _enemyPrefab; [SerializeField] private GameObject _enemyContainer; [SerializeField] private GameObject[] _powerups; [SerializeField] private GameObject _powerupContainer; private bool _stopSpawning; // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { } public void StartSpawning() { StartCoroutine(SpawnEnemyRoutine()); StartCoroutine(SpawnPowerups()); } public IEnumerator SpawnEnemyRoutine() { yield return new WaitForSeconds(3.0f); while (_stopSpawning == false) { GameObject newEnemy = Instantiate(_enemyPrefab, RandomizeNewPosition(), Quaternion.identity); newEnemy.transform.parent = _enemyContainer.transform; yield return new WaitForSeconds(5); } } private IEnumerator SpawnPowerups() { yield return new WaitForSeconds(3.0f); while (_stopSpawning == false) { int randomPowerup = Random.Range(0, 3); GameObject powerup = Instantiate(_powerups[randomPowerup], RandomizeNewPosition(), Quaternion.identity); powerup.transform.parent = _powerupContainer.transform; float randomTime = Random.Range(3f, 7f); yield return new WaitForSeconds(randomTime); } } public void OnPlayerDeath() { _stopSpawning = true; } private Vector3 RandomizeNewPosition() { float randomX = Random.Range(-9f, 9f); return new Vector3(randomX, 7.5f, 0); } }