Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Health bar does not update (value stays same all the time)[FIXED]

Discussion in 'Scripting' started by moynzy, Oct 30, 2014.

  1. moynzy

    moynzy

    Joined:
    Oct 22, 2014
    Posts:
    82
    Hey guys, first of all I want to credit BurgZerg Arcade for making a health bar tutorial, and second the live unity tutorials for the teachings.

    I have a save and load feature in a static class called GameControl which stores the players details, such as health, mana, etc in to a file.


    Code (CSharp):
    1. public class PlayerHealth : MonoBehaviour {
    2.     public int maxHealth = GameControl.control.health;
    3.     public int curHealth = GameControl.control.health;
    4.    
    5.     public float healthBarLength;
    6.  
    7.     void Awake(){
    8.         GameControl.control.Load();
    9.         Debug.Log(GameControl.control.health);
    10.         Debug.Log(maxHealth);
    11.     }
    Basically, I've used debug to get my values and the first debug gets 72 ( since I've set health to 72).
    But the maxHealth displays 32 (the first value that was set before 72).

    My question is, shouldn't maxHealth be set to 72, since Game.control.health = 72.

    It's still stuck on 32, and this object doesn't get destroyed.

    Thanks for your time, if you need more information please do ask.
    Much appreciated, in advance.
     
  2. laurelhach

    laurelhach

    Joined:
    Dec 1, 2013
    Posts:
    229
    Is it possible that your variable maxHealth is assigned before you load your values?
    What is also possible is that your maxHealth has a value in the inspector that is different from 72, so it never gets updated after (I wouldn't be surprised you have 32 in the inspector of your script object for your maxHealth).
    If you debug your curHealth, what do you get?
     
  3. Zaladur

    Zaladur

    Joined:
    Oct 20, 2012
    Posts:
    392
    You are assigning maxHealth before runtime. I am not sure what GameControl.control.Load does but I am guessing it can update the value of GameControl.control.health? Maxhealth is never going to update itself automatically.

    Seems like you want maxHealth to equal control's values after loading - explicitly place that line in there, not up at the variable declaration.