using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class HealthUI : MonoBehaviour { public Image heartPrefab; public Sprite fullHeartSprite; public Sprite emptyHeartSprite; private List hearts = new List(); public void SetMaxHearts(int maxHearts) { if (this == null) return; // Destroy and clear existing hearts foreach (var heart in hearts) { if (heart != null) Destroy(heart.gameObject); } hearts.Clear(); // Create new hearts for (int i = 0; i < maxHearts; i++) { Image newHeart = Instantiate(heartPrefab, transform); newHeart.sprite = fullHeartSprite; newHeart.color = Color.red; hearts.Add(newHeart); } } public void UpdateHearts(int currentHealth) { for (int i = 0; i < hearts.Count; i++) { if (hearts[i] == null) continue; if (i < currentHealth) { hearts[i].sprite = fullHeartSprite; hearts[i].color = Color.red; } else { hearts[i].sprite = emptyHeartSprite; hearts[i].color = Color.white; } } } }