using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement; public class PlayerHealth : MonoBehaviour { public int maxHealth = 5; //Set player max health [SerializeField] Slider healthSlider; public int currentHealth; void Update() { if (currentHealth <=0) { Die(); } } void Start() { currentHealth = maxHealth; //Start at max hp UpdateHealthUI(); } public void TakeDamage(int damage) //Damage logic { currentHealth -= damage; currentHealth = Mathf.Clamp(currentHealth, 0, maxHealth); UpdateHealthUI(); } public void Heal(int amount) //Heal logic { currentHealth += amount; currentHealth = Mathf.Clamp(currentHealth, 0, maxHealth); UpdateHealthUI(); } void UpdateHealthUI() { if (healthSlider != null) { healthSlider.maxValue = maxHealth; healthSlider.value = currentHealth; } } void Die() { SceneManager.LoadScene("End UI"); } }