Search Unity

How do i make a GUI.Label Display a float?

Discussion in 'Immediate Mode GUI (IMGUI)' started by spiralgear, Apr 20, 2008.

  1. spiralgear

    spiralgear

    Joined:
    Dec 13, 2007
    Posts:
    528
    Im trying to make a health meter, i get an error when i put the health var into a label
     
  2. TJB

    TJB

    Joined:
    Mar 20, 2007
    Posts:
    146
    Just convert it to a string like this:
    Code (csharp):
    1. var health = 10.0;
    2.  
    3. function OnGUI () {
    4.      var healthstring = health.ToString();
    5.      GUI.Label(Rect(10,10,100,30), healthstring);
    6. }
     
  3. spiralgear

    spiralgear

    Joined:
    Dec 13, 2007
    Posts:
    528
    sweet thanks alot
     
  4. jashan

    jashan

    Joined:
    Mar 9, 2007
    Posts:
    3,307
    Not sure if that works as well in UnityScript (Unity's JavaScript), but in C#, you could also do

    Code (csharp):
    1.  
    2. floatVariable.ToString("#.00");
    3.  
    If you want it to be rendered to two digits after the dot. So that would give you, e.g. 3.23 or 123.56 instead of something like 2.5323423523 which doing ToString() might do...

    Sunny regards,
    Jashan
     
  5. spiralgear

    spiralgear

    Joined:
    Dec 13, 2007
    Posts:
    528
    actually i ran into that problem right away,the extra digits were making my automatic layout Label bigger that its box, ill try that later


    thanks
     
  6. jeremyace

    jeremyace

    Joined:
    Oct 12, 2005
    Posts:
    1,661