using UnityEngine; using UnityEngine.SceneManagement; /// /// Manages game state and responds to core destruction. /// public class GameManager : MonoBehaviour { [SerializeField] private Core coreReference; private bool _isGameOver = false; private HealthComponent _coreHealth; private void Start() { // Find Core if not assigned if (coreReference == null) { coreReference = FindFirstObjectByType(); } // Subscribe to core's health component if (coreReference != null) { _coreHealth = coreReference.Health; if (_coreHealth != null) { _coreHealth.OnDeath += GameOver; } } } private void OnDestroy() { // Unsubscribe to prevent memory leaks if (_coreHealth != null) { _coreHealth.OnDeath -= GameOver; } } private void GameOver() { if (_isGameOver) return; _isGameOver = true; Debug.Log("Game Over! Core has been destroyed."); // Show defeat UI here // Example: restart game after 3 seconds Invoke(nameof(RestartGame), 3f); } private void RestartGame() { SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex); } }