Health bar script not working, bar not reducing with player's vitality

I need a health bar that decreases proportionately with the player’s vitality, but the script has given the error “An instance of type ‘healthBar’ is required to access non static member ‘fgImage’”.
I have been through the forums (I know that there are so many health bar related questions) - but I still can’t work out how to make the health bar work properly.

Here is the health bar script:

var energyBar : GUIStyle ;
   
    var bgImage : Texture2D; 
    var fgImage : Texture2D; 
    static var playerEnergy = 1.0;     
    var maxHealth : int = 100.0;
    var curHealth : int = 70.0;
    var percentHealth : Number = curHealth / maxHealth;
     
    function Start() {
  
    }
     
    function Update() {
    
    }
     
    function OnGUI () {

    GUI.BeginGroup (Rect (10,10,256,32));
     
    GUI.Box (Rect (0,0,256,32), bgImage, energyBar);
     
    GUI.BeginGroup (Rect (0,0,playerEnergy * 256, 32));
     
    GUI.Box (Rect (0,0,256,32), fgImage, energyBar);
     
    GUI.EndGroup ();
    GUI.EndGroup ();
    }
    
    function updateHealthBar () : void
    {
    percentHealth = curHealth / maxHealth;
    healthBar.fgImage.ScaleX = percentHealth;
    }

If anyone has the time to point out how to sort out the errors I would be so grateful.
Thanks in advance, Laurien

I think a little more information will help. Please post the entire error from the console.

Assets/Game Scripts/healthBar1.js(48,11):BCE0020:An instance of type 'healthBar' is required to access non static member 'fgImage'. The bottom line seems to have worked for lots of people on the forum - but I can't work out what's wrong.

2 Answers

2

Try change the maxHealth and curHealth to floats.

Unity doesn’t like dealing with integers for percentages.

— Then —

Replace your healthBar.fgImage.ScaleX

to

healthBar.fgImage.transform.scale.x

Actually, your 'healthBar.fgImage.ScaleX' might be okay - try changing the ints though

Oh thanks - I didn't realise about the integers! (that problematic line is still throwing errors though)

OK. The debug line says exactly what is wrong. You do not have an instance of healthBar available so your code is accessing fgImage as a static variable. Instead of:

healthBar.fgImage.ScaleX = percentHealth;

try this:

fgImage.ScaleX = percentHealth;

Hopefully that will remedy the static reference error.

This is embarrassing annoying everyone with errors. The static reference error is gone!! (thanks - I understand what it means now!!) but now I have the error: Assets/Game Scripts/healthBar1.js(49,9): BCE0019: 'ScaleX' is not a member of 'UnityEngine.Texture2D'. I've checked through the forums for similar problems - but the solutions they've used don't work.

Glad We are here to help! The main thing is that you are trying and learning, so keep it up! Check out the documentation for Texture2D. http://docs.unity3d.com/Documentation/ScriptReference/Texture2D.html There does not appear to be a ScaleX function for Texture2D instances. I have not worked with this class much, but you might try looking at the Resize function. http://docs.unity3d.com/Documentation/ScriptReference/Texture2D.Resize.html if that does not help try opening a new question specifically for that problem!

Thanks so much - I'm just looking through the resize function now :)