using UnityEngine; using UnityEngine.AI; /// /// Gate structure that can be destroyed. /// Uses HealthComponent for health management. /// [RequireComponent(typeof(HealthComponent))] public class Gate : MonoBehaviour { private HealthComponent _health; private NavMeshObstacle _obstacle; void Awake() { _health = GetComponent(); _obstacle = GetComponent(); // Subscribe to health component events _health.OnDeath += HandleDeath; } void OnDestroy() { if (_health != null) { _health.OnDeath -= HandleDeath; } } private void HandleDeath() { // Disable the gate visually gameObject.SetActive(false); // Disable NavMesh carving so enemies can pass through if (_obstacle != null) { _obstacle.carving = false; _obstacle.enabled = false; } // Destroy the object after a short delay Destroy(gameObject, 0.1f); } /// /// Get the health component for direct access. /// public HealthComponent Health => _health; }