Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Unity 4.6 UI change image fill amount with JavaScript / Unity Script

Discussion in 'UGUI & TextMesh Pro' started by Marque-Irving, Jan 3, 2015.

  1. Marque-Irving

    Marque-Irving

    Joined:
    Dec 26, 2012
    Posts:
    8
    Basically as the title says I would like to know how to change the Fill Amount of a UI image with JavaScipt / Unity Script. I've looked at the documentation and it doesn't really help. The image currently acts as a health bar
     
    Last edited: Jan 3, 2015
  2. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    Java is a language developed by Oracle, not Unity.
     
  3. Marque-Irving

    Marque-Irving

    Joined:
    Dec 26, 2012
    Posts:
    8
    You can use Java in Unity no? or should I refer to it as Unity Script instead?
     
  4. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    Where did you get the impression that Unity supported Java?
     
  5. Marque-Irving

    Marque-Irving

    Joined:
    Dec 26, 2012
    Posts:
    8
  6. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    No.

    Yes. You could call also it Javascript, since that's what the docs call it (note that Javascript has nothing whatsoever to do with Java). But ideally you should call it Unityscript, since it's a custom language that's not really compatible with Javascript.

    --Eric
     
  7. Marque-Irving

    Marque-Irving

    Joined:
    Dec 26, 2012
    Posts:
    8
    Thank you, my apologies. I will Keep note of this...But can anyone answer my question please
     
  8. Marque-Irving

    Marque-Irving

    Joined:
    Dec 26, 2012
    Posts:
    8
    I got it working in case anyone wanted to know, here was the code from my test:

    Code (JavaScript):
    1.  
    2. public var healthBar : UnityEngine.UI.Image;  //Tells Unity that the gameObject is a UI Image
    3. public var fillAmount: float; //Fill Amount for UI Image
    4.  
    5. function OnTriggerEnter () { //When character enters trigger
    6.     Debug.LogWarning("Losing Health"); //Just so I know the trigger was working
    7.  
    8.     healthBar.fillAmount = (healthBar.fillAmount - 0.50); //for everytime the character enters the trigger it will lose half health
    9.     }
    10.  
    11. function Update () {
    12.  
    13. }
     
    Last edited: Jan 4, 2015
    gmercade likes this.
  9. gmercade

    gmercade

    Joined:
    Jun 3, 2016
    Posts:
    3
    Man u saved me.

    Thanks a lot !