using System.Collections; using System.Collections.Generic; using UnityEngine; using Utility; public class ZombieSpawner : MonoBehaviour { [SerializeField] GameObject ZombiePrefab; [SerializeField] private float _spawnRate = 2.0f; [SerializeField, ShowOnly] private float _spawnTimer; private float _secondsPerAliveTime; // Start wird aufgerufen, bevor das erste Frame gezeichnet wird void Start() { _secondsPerAliveTime = GetComponentInParent().ActiveTimeSpanInSeconds; _spawnTimer = Random.Range(0.5f * _secondsPerAliveTime / _spawnRate, _secondsPerAliveTime / _spawnRate); } // Update wird einmal pro Frame aufgerufen void Update() { _spawnTimer -= Time.deltaTime; if (_spawnTimer <= 0) { Instantiate(ZombiePrefab, transform.position, Quaternion.identity, transform); _spawnTimer = _secondsPerAliveTime / _spawnRate; } } private void OnEnable() { if (GameManager.Instance != null) { if (GameManager.Instance.ContextBuffer != null) GameManager.Instance.AddContext("The Developer informs Gottfried that Zombies appeared outside so Gottfried better not leave the office"); } } private void OnDisable() { if (GameManager.Instance.ContextBuffer != null) GameManager.Instance.RemoveContext("The Developer informs Gottfried that Zombies appeared outside so Gottfried better not leave the office"); // Destroy all children (zombies) for (int i = 0; i < transform.childCount; i++) { KillZombiesByDisable(); } } private void KillZombiesByDisable() { Destroy(transform.GetChild(i).gameObject); } }