Search Unity

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

Discussion in 'Scripting' started by Sylvir, Jul 6, 2015.

  1. Sylvir

    Sylvir

    Joined:
    Mar 21, 2015
    Posts:
    396
    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.

    Code (CSharp):
    1. void Update ()
    2.     {
    3.         Image = GetComponent<Image>();
    4.        
    5.         Image.fillAmount = GameControl.control.currentLoggingXP / GameControl.control.requierdLoggingXP;
    6.     }
    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>() ?
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    Code (csharp):
    1.  
    2. Image myImage = GetComponent<Image>();
    3. myImage.fillAmount ... ...
    4.  
    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.
     
    Sylvir likes this.
  3. Sylvir

    Sylvir

    Joined:
    Mar 21, 2015
    Posts:
    396
    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
     
  4. Sylvir

    Sylvir

    Joined:
    Mar 21, 2015
    Posts:
    396
    I worked it out. in case anyone else is looking for this the script that I used was this..

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class woodCuttingProgressBar : MonoBehaviour {
    6.  
    7.     public Image image;
    8.    
    9.     void Start()
    10.     {
    11.         image = GetComponent<Image>();
    12.     }
    13.    
    14.     // Update is called once per frame
    15.     void Update ()
    16.     {
    17.        
    18.         image.fillAmount = GameControl.control.currentLoggingXP / GameControl.control.requierdLoggingXP;
    19.     }
    20. }
    21.  
    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)
     
  5. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    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.
     
  6. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Appropriate to put in an "I told you so" placeholder for later? Static data classes will give you trouble.
     
  7. Sylvir

    Sylvir

    Joined:
    Mar 21, 2015
    Posts:
    396
    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. :p
     
    Last edited: Jul 7, 2015