I used a slider instead to figure %

I took a different approach to showing the percentage using a slider…

EDIT - Loading your health will always appear at 100% because of the SetSliderMaximumValue() method pulling the same variable in as the SetSliderValue() method namely GetHealthPoints();
In later lectures after putting systems in place to call stats based on class and level… the corrected script appears below… or you could just set that max value at say 100 for now until you can replace it with the corrected script…

Corrected Script that calls the max value appropriate to your player level
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using RPG.Combat;
using RPG.Stats;

namespace RPG.Resources
{
    public class HealthDisplay : MonoBehaviour
    {
        [Space(10)]
        [Header("Player Health")]
        [SerializeField] Slider healthSlider;
        [SerializeField] TextMeshProUGUI healthText;
        Health healthScript;
        Fighter fighterScript;
        BaseStats baseStatsScript;

        [Space(10)]
        [Header("Enemy Health")]
        [Space(20)]
        [SerializeField] GameObject enemyHealthDisplay;
        [SerializeField] Slider enemyHealthSlider;
        [SerializeField] TextMeshProUGUI enemyHealthText;
        [SerializeField] GameObject backingPanel;
        Health enemyHealth;

        private void Awake()
        {
            healthScript = GameObject.FindWithTag("Player").GetComponent<Health>();
            fighterScript = healthScript.gameObject.GetComponent<Fighter>();
            baseStatsScript = healthScript.gameObject.GetComponent<BaseStats>();
        }

        void Start()
        {
            SetSliderMaximumValue();
        }

        private void SetSliderMaximumValue()
        {
            healthSlider.maxValue = baseStatsScript.GetStatInProgressionSO(Stat.Health); // or call GetStat() in Progression.cs
        }

        void Update()
        {
            SetSliderValue();
            SetHealthText();

            if(fighterScript.GetTarget() != null)
            {
                AssignEnemyToEnemyHealth();
                SetEnemySliderMaximumValue();
                SetEnemySliderValue();
                SetEnemyHealthText();
            }
            else
            {
                enemyHealth = null;
                enemyHealthDisplay.SetActive(false);
                backingPanel.SetActive(false);
            }
        }


        // PLAYER
        private void SetSliderValue()
        {
            healthSlider.value = healthScript.GetHealthPoints();
        }

        private void SetHealthText()
        {
            var healthPercent = (healthSlider.value / healthSlider.maxValue * 100).ToString();
            healthText.text = string.Format("{0:0.0}%", healthPercent);
        }



        // ENEMY
        private void AssignEnemyToEnemyHealth()
        {
            enemyHealth = fighterScript.GetTarget().GetComponent<Health>();
            enemyHealthDisplay.SetActive(true);
            backingPanel.SetActive(true);
        }

        private void SetEnemySliderMaximumValue()
        {
            enemyHealthSlider.maxValue = enemyHealth.gameObject.GetComponent<BaseStats>().GetStatInProgressionSO(Stat.Health);
        }

        private void SetEnemySliderValue()
        {
            enemyHealthSlider.value = enemyHealth.GetHealthPoints();
        }

        private void SetEnemyHealthText()
        {
            var enemyHealthPercent = (enemyHealthSlider.value / enemyHealthSlider.maxValue * 100).ToString();
            enemyHealthText.text = string.Format("{0:0.0}%", enemyHealthPercent);
        }
    }
}

be sure to check “use whole numbers”..

here’s the code…

using UnityEngine;
using UnityEngine.UI;
using TMPro;
using System.Collections;

namespace RPG.Resources
{
    public class HealthDisplay : MonoBehaviour
    {
        Health health;
        [SerializeField] Slider healthSlider;
        [SerializeField] TextMeshProUGUI healthText;

        private void Awake()
        {
            health = GameObject.FindWithTag("Player").GetComponent<Health>();
        }

        IEnumerator Start()
        {
            yield return new WaitForSeconds(0.1f);
            SetSliderMaximumValue();
        }

        private void SetSliderMaximumValue()
        {
            healthSlider.maxValue = health.GetHealthPoints();
        }

        void Update()
        {
            SetSliderValue();
            SetHealthText();
        }

        private void SetSliderValue()
        {
            healthSlider.value = health.GetHealthPoints();
        }

        private void SetHealthText()
        {
            healthText.text = (healthSlider.value / healthSlider.maxValue * 100).ToString() + "%";
        }
    }
}
5 Likes

Privacy & Terms