using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class HealthBar : MonoBehaviour { public Slider healthBarSlider; Damageable playerDamageable; //public string objectTag; public GameObject player; private void Awake() { GameObject player = this.player; playerDamageable = player.GetComponent(); } // Start is called before the first frame update void Start() { healthBarSlider.value = CalculateSliderPercentage(playerDamageable.Health, playerDamageable.MaxHealth); } private void OnEnable() { playerDamageable.healthChanged.AddListener(OnPlayerHealthChanged); } private void OnDisable() { playerDamageable.healthChanged.RemoveListener(OnPlayerHealthChanged); } private float CalculateSliderPercentage(float currentHealth, float maxHealth) { return currentHealth / maxHealth; } private void OnPlayerHealthChanged(int newHealth, int newMaxHealth) { healthBarSlider.value = CalculateSliderPercentage(newHealth, newMaxHealth); } }