trying to make a fill image work as an experience bar. here is what i have so far..

hey,

I am attempting to use one of the slider images to work as an experience bar type of a deal.
here is the code i came up with so far.

void Update ()
    {
        Image = GetComponent<Image>();
       
        Image.fillAmount = GameControl.control.currentLoggingXP / GameControl.control.requierdLoggingXP;
    }

looked good when i saved it but then its saying that an object reference is required to access non static member unityEngine.UI.Image.fullAmount do i need to reference the object image another way then "image = getcomponent() ?

Image myImage = GetComponent<Image>();
myImage.fillAmount ... ...

not sure what GameControl is in your project, but if it’s not a static class that’ll have issues along the same lines too

oh and really you should be doing the “GetComponent” part in the Start function and have a class variable for it, you don’t want to be looking up the Image in the hierarchy every frame if it’s going to always be there.

1 Like

thanks for the tip and answer! Game Control actually is public and static tho… that is why im confused… its what im using to store all my variables im saving so that they can be accessed from anywhere

I worked it out. in case anyone else is looking for this the script that I used was this…

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

public class woodCuttingProgressBar : MonoBehaviour {

    public Image image;
   
    void Start()
    {
        image = GetComponent<Image>();
    }
   
    // Update is called once per frame
    void Update ()
    {
       
        image.fillAmount = GameControl.control.currentLoggingXP / GameControl.control.requierdLoggingXP;
    }
}

works just as expected. if anyone needs to make a health or experience bar this is one way (all though I am sure there are better ways this is just how i worked it out with the unity documentation and some tips from people on here)

you can use stripped down UI.sliders for these things too, you have to delete the drag handle child and reset a few positional variables in the inspectors. But if you set it to non-interactable you can then use it’s value attribute as the % slider.

Appropriate to put in an “I told you so” placeholder for later? Static data classes will give you trouble.

you can if you want, but every place ive looked says the same thing… make one control scirpt where everything you are saving is stored then pull everything from that. Not really sure why every place i look says to do that if it causes trouble… (that is me doubting them not you) i wish that somewhere it would explain a better way to do it if that is such a problematic way of doing things.

EDIT : even the unity documentation video explains to do it that way, this is why im confused. :stuck_out_tongue: