using UnityEngine; public class Health : MonoBehaviour { [SerializeField] private float startingHealth; public float currentHealth { get; private set; } private Animator anim; private bool isDead; private AudioManager audioManager; private PlayerRespawn playerRespawn; public GameManagerScript gameManager; private PlayerMovement playerMovement; private void Awake() { currentHealth = startingHealth; anim = GetComponent(); audioManager = GameObject.FindGameObjectWithTag("Audio")?.GetComponent(); playerRespawn = GetComponent(); playerMovement = GetComponent(); // Debug logs to check for null references Debug.Log($"Animator: {anim}"); Debug.Log($"AudioManager: {audioManager}"); Debug.Log($"PlayerRespawn: {playerRespawn}"); Debug.Log($"PlayerMovement: {playerMovement}"); Debug.Log($"GameManager: {gameManager}"); } public void TakeDamage(float damage) { currentHealth = Mathf.Clamp(currentHealth - damage, 0, startingHealth); if (currentHealth > 0) { if (audioManager != null) { audioManager.PlaySFX(audioManager.hurt); } anim.SetTrigger("hurt"); } else if (!isDead) { isDead = true; if (gameManager != null) { gameManager.gameOver(); } if (audioManager != null) { audioManager.PlaySFX(audioManager.death); } anim.SetTrigger("die"); // Disable player or enemy movement here DisableMovement(); // Additional logic for enemy controls, etc. } } private void DisableMovement() { // Disable the EnemyPatrol script var enemyPatrol = GetComponent(); if (enemyPatrol != null) { enemyPatrol.enabled = false; } // Disable the Rigidbody2D if present var rb = GetComponent(); if (rb != null) { rb.velocity = Vector2.zero; rb.isKinematic = true; } // Stop the enemy's movement animation if (anim != null) { anim.SetBool("moving", false); } } public void AddHealth(float value) { if (audioManager != null) { audioManager.PlaySFX(audioManager.life); } currentHealth = Mathf.Clamp(currentHealth + value, 0, startingHealth); } private void Respawn() { if (playerRespawn != null) { playerRespawn.Respawn(); currentHealth = startingHealth; isDead = false; // Re-enable player controls by setting IsDead to false if (playerMovement != null) { playerMovement.IsDead = false; } } } public void RestartFromButton() { if (playerRespawn != null) { playerRespawn.Respawn(); currentHealth = startingHealth; isDead = false; // Re-enable player controls by setting IsDead to false if (playerMovement != null) { playerMovement.IsDead = false; } } } }