Where/when does Sam implement the health update when we level up?

Hello,

At around 11:00 Sam kills an enemy and his health percentage doesn’t change.

At around 13:15 Sam kills an enemy and his health percentage drops (indicating that his health/maxhealth has changed from leveling up).

The only change to the code between to the two timestamps is adding GetLevel() inside of Getstat();

This indicates that GetStat is being called somewhere when he levels up.

THE PROBLEM: I am not getting this level up and health changing effect.

I am only calling BaseStats.GetStat() in two other places in code, both in the health script. First reference is in the start function of Health that calls GetStat to set the characters health at the start. The second reference is GetStat providing the experience reward to gain experience.

How is Sam’s character’s health updating when he levels up? In what part of the code does this happen? Also is there a repository to see all of his code at this point in the class? I can see his changes under the video but not a full copy of his code to check this myself.

Thank you,

Michael

P.S. I already made a solution where when an enemy dies and awards experience it also calls a function on the instigator (player) that updates the players health component to have a new max health calling GetStat to do so. I follow along pretty closely but I can not find out where Sam added the code to update the players health so I wanted to compare his solution to my own now.

Sam’s solution is to have the health display and health bar update the status in their respective Update() methods.

public class HealthBar : MonoBehaviour
    {
        [SerializeField] Health healthComponent = null;
        [SerializeField] RectTransform foreground = null;
        [SerializeField] Canvas rootCanvas = null;


        void Update()
        {
            if (Mathf.Approximately(healthComponent.GetFraction(), 0)
            ||  Mathf.Approximately(healthComponent.GetFraction(), 1))
            {
                rootCanvas.enabled = false;
                return;
            }

            rootCanvas.enabled = true;
            foreground.localScale = new Vector3(healthComponent.GetFraction(), 1, 1);
        }
    }

This means that as soon as you level up, the health bar will be adjusted automatically.

Hi Brian, the code you show above is used in this lecture? I never saw that code yet?

Also my question is where in the code are we increasing our total or max health? I have been following along and in this lecture between the 11:00 and 13:15 timestamps we go from killing an enemy not changing health at all to seeing the “bug” that the health percentage jumps down when you level up. Where is the code that makes our health increase as we level up? I did not seeing it in the course was it skipped or am I missing it?

The script I mentioned above has it’s origins in the lecture “Updating the Health Bar Scale”.

We’re not actually storing the maximum health directly once we get to levelling characters. Instead, we get it from GetComponent<BaseStats>().GetStat(Stat.Health);

So GetFraction() in Health returns the current health divided by GetStat(Stat.Health), rather than directly caching the result. This means that once we level up, this calculation will automatically reflect the updated maximum health.

This topic was automatically closed after 32 hours. New replies are no longer allowed.