Search Unity

Image disappearing when assigning fill amount

Discussion in 'UGUI & TextMesh Pro' started by moodiey, Mar 7, 2017.

  1. moodiey

    moodiey

    Joined:
    Jan 4, 2017
    Posts:
    16
    Hello,

    I have a standard circular "ammo bar" under my UI canvas that i want to track the amount of ammo i have left defined in my gamemaster script

    The problem is that once I fire, subtracting the ammo by 1... the fill amount goes to 0 and the image disappears. I've checked the math, and debugged the values in real time... scripts are communicating just fine... but for some reason the fill amount just goes to zero once i fire the first bullet. Heres my code applied to directly to the image under canvas:

    Code (CSharp):
    1.      public static Image ammoCounter;
    2.      private GameObject gm;
    3.      void Start () {
    4.          ammoCounter = GetComponent<Image>();
    5.          gm = GameObject.FindGameObjectWithTag("gamemaster");
    6.      }
    7.    
    8.      void FixedUpdate () {
    9.          ammoCounter.fillAmount = gm.GetComponent<gamemaster>().foodAmmo / 50;
    10.          print(gm.GetComponent<gamemaster>().foodAmmo);
    11.      }
    12. }

    my food ammo value in gamemaster is assigning fine... I assign it to 50 in the inspector.

    Any help?
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    The trouble with integer math is that it returns an integer, even when dividing.
    Code (csharp):
    1.  
    2.          ammoCounter.fillAmount = ( (float)gm.GetComponent<gamemaster>().foodAmmo) / 50f;
    3.  
     
    CanisMajor and moodiey like this.
  3. moodiey

    moodiey

    Joined:
    Jan 4, 2017
    Posts:
    16
    Yes!

    that makes sense and works perfectly. Thanks a bunch