UI Stamina bar is not working after building

Hi everyone,

I created a game where I have stamina and hence a stamina bar.
While playing in the editor, the stamina bar works fine and decreases every time I jump.

After building the game, the bar is shown but isn’t adjusting. The stamina is changed though.

Here is the stamina bar code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class LifeBar : MonoBehaviour
{

    private const float MAX_HEALTH = 100f;

    public float currentStamina = MAX_HEALTH;

    private Image healthBar;

    // Start is called before the first frame update
    void Start()
    {
        healthBar = GetComponent<Image>();
    }

    // Update is called once per frame
    void Update()
    {
        currentStamina = GameObject.Find("Dragon").GetComponent<DragonMovement>().stamina;
        healthBar.fillAmount = currentStamina / MAX_HEALTH;
    }
}

My guess is that your “Find” function is not finding the “Dragon” for some reason, causing your Update code to abort with a Null Reference Exception. Possibly because your build started in a different scene than you did in the editor, or some other difference in early program flow.

By the way, "Find’ is kind of expensive, so calling it every frame in Update is generally a bad idea. It would be a lot more efficient if you found it once (say, in Start) and then saved the DragonMovement component in a variable so that you don’t have to go find it again every frame.