using UnityEngine; using UnityEngine.UI; [RequireComponent(typeof(Slider))] public class UIHUDHealthBar : MonoBehaviour { public Text nameField; public Image playerPortrait; public Slider HpSlider; public bool isPlayer; public int playerNumber; void OnEnable() { HealthSystem.onHealthChange += UpdateHealth; } void OnDisable() { HealthSystem.onHealthChange -= UpdateHealth; } void Start(){ if(!isPlayer) Invoke("HideOnDestroy", Time.deltaTime); //hide enemy healthbar at start if(isPlayer) SetPlayerPortraitAndName(); } void UpdateHealth(float percentage, GameObject go){ if(isPlayer && go.CompareTag("Player") && go.GetComponent()?.playerNumber == playerNumber){ HpSlider.value = percentage; } if(!isPlayer && go.CompareTag("Enemy")){ HpSlider.gameObject.SetActive(true); HpSlider.value = percentage; nameField.text = go.GetComponent().enemyName; if(percentage == 0) Invoke("HideOnDestroy", 2); } } void HideOnDestroy(){ HpSlider.gameObject.SetActive(false); nameField.text = ""; } //loads the HUD icon of the player from the player prefab (Healthsystem) void SetPlayerPortraitAndName(){ if(playerPortrait != null){ GameObject[] players = GameObject.FindGameObjectsWithTag("Player"); GameObject player = null; foreach (GameObject p in players) { if (p.GetComponent()?.playerNumber == playerNumber) player = p; } HealthSystem healthSystem = player?.GetComponent(); if(player && healthSystem != null){ //set portrait Sprite HUDPortrait = healthSystem.HUDPortrait; playerPortrait.overrideSprite = HUDPortrait; //set name nameField.text = healthSystem.PlayerName; } else { nameField.text = "PRESS START!"; playerPortrait.enabled = false; HpSlider.gameObject.SetActive(false); } } } }