Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved Fill amount updates value but not image

Discussion in 'Scripting' started by GrayedFox, Jan 29, 2021.

  1. GrayedFox

    GrayedFox

    Joined:
    Jan 28, 2014
    Posts:
    70
    Got a really weird bug I can't put my finger on. Here is method tied to an input (the E key on my keyboard) that reduces player health and stamina:

    Code (CSharp):
    1.     public void Act(float actTime)
    2.     {
    3.         CurrentHealth -= 10;
    4.         CurrentStamina -= 10;    // comment me out to break health bar
    5.     }
    This method is called every frame from the GUI manager for each player in the scene:

    Code (CSharp):
    1.     // reflect player's current health and mana to their health and mana bars
    2.     public void ReflectResources()
    3.     {
    4.         Debug.Log($"health fill amount value: {HealthBar.fillAmount}");
    5.         HealthBar.fillAmount = Player.CurrentHealth / Player.MaxHealth;
    6.         HealthText.text = $"{Mathf.Floor(Player.CurrentHealth)}";
    7.  
    8.         ManaBar.fillAmount = Player.CurrentMana / Player.MaxMana;
    9.         ManaText.text = $"{Mathf.Floor(Player.CurrentMana)}";
    10.  
    11.         StaminaBar.fillAmount = Player.CurrentStamina / Player.MaxStamina;
    12.         StaminaText.text = $"{Mathf.Floor(Player.CurrentStamina)}";
    13.     }
    The weird thing is, the current health and stamina values do indeed update, but the bar remains filled for the stamina bar (not the health bar).

    Again, to be clear: the actual health and stamina of the player is updating, since the text displayed is correct and debugging output confirms this. It's only the displayed visual fill value that does not update.

    Even stranger still: if I comment out the
    CurrentStamina -= 10;
    line, then the health bar fill amount doesn't update (but the underlying value does).

    The log also displays the correct value for the fill amount - so it seems like the fill amount is indeed updating - but again, visually, the bar remains full.

    Here, the health bar updates correctly, because
    CurrentStamina -= 10;
    is not commented out. As you can see, the stamina bar remains full despite only being 70% full and exhibits the bug.
    with_stamina_reduction.png

    Here the health bar exhibits the bug, because
    CurrentStamina -= 10;
    is commented out.
    without_stamina_reduction.png

    What am I missing? I've tried calling ReflectResources() from FixedUpdate and also LateUpdate, but that also didn't work.
     
    Last edited: Jan 29, 2021
  2. Terraya

    Terraya

    Joined:
    Mar 8, 2018
    Posts:
    646
    so if the "UI" doesnt update correctly but the rest does,

    check please your "UI" Components if they are the same way setup AND if you assign them correctly,

    it looks to me that you try something like "Find Object with Component" or "tryGetComponent" or something similar, and that he gets the wrong UI
     
  3. GrayedFox

    GrayedFox

    Joined:
    Jan 28, 2014
    Posts:
    70
    That's a good suggestion, but I assign the references statically and have indeed made sure each Bar game object is set up the same way.

    That also wouldn't explain why commenting out the stamina reduction "moves" the bug to the health bar (since the variable assignment, in this case, hasn't changed). As you can see, the fill origin, method, and type are all set correctly and are identical between the health, mana, and stamina bars.

    The display logic is also identical (the ReflectResources method). Any other ideas my friend?

    Identical setup (except for colour):
    identical-setup.png static_refs.png

    Static reference: static_refs.png
     
  4. GrayedFox

    GrayedFox

    Joined:
    Jan 28, 2014
    Posts:
    70
    Omg dude. I found it. Mother flipping copy pasta:

    Code (CSharp):
    1.     private void SetUpBars() {
    2.         HealthBar = Health.transform.GetChild(0).GetComponent<Image>();
    3.         HealthText = Health.transform.GetChild(2).GetComponent<TextMeshProUGUI>();
    4.  
    5.         ManaBar = Mana.transform.GetChild(0).GetComponent<Image>();
    6.         ManaText = Mana.transform.GetChild(2).GetComponent<TextMeshProUGUI>();
    7.  
    8.         StaminaBar = Health.transform.GetChild(0).GetComponent<Image>();
    9.         StaminaText = Stamina.transform.GetChild(2).GetComponent<TextMeshProUGUI>();
    10.  
    11.         CastBar = Cast.transform.GetChild(0).GetComponent<Image>();
    12.         CastText = Cast.transform.GetChild(1).GetComponent<TextMeshProUGUI>();
    13.  
    14.         // make the cast bar follow the player
    15.         Cast.GetComponentInChildren<FollowUnit>().Init(Player.gameObject);
    16.     }
    Check the stamina bar assignment :oops:

    Thanks for your input, made me rubber duck myself.
     
    Terraya likes this.