- Home /
 
need a little help converting a float var to a .ToString
I know there is a way to do it, but i can not remember and i can not find the info anywhere that would help me. I saw a few examples but none were what i needed mainly it is the last line of code. i want to convert the variable batteryLife to a string, but that line with batteryLife.ToString gives me an error.
thanks
 #pragma strict
 var batteries: float = 2.0f;
 var batteryLife: float = 420.0f;
 var batteryReductionSpeed: float = 1.0f;
 
 var lightRange: float = 100.0f;
 
 var horizontalOffset01: float = 175f;
 var verticalOffset01: float = 0.47f;
 
 var horizontalOffset02: float = 0.37f;
 var verticalOffset02: float = 0.4f;
 
 var horizontalOffset03: float = 0.37f; //701
 var verticalOffset03: float = -100f;    // 0.72
 
 //var guiLifeOfBattery = guiText;
 //var guiRemainingBatLife = guiText;
 
 
 
 var instruction = "To Turn Flashlight on or off, press O";
 var words = false;
 
 
 
 function Awake () 
 {
     
     GameObject.FindWithTag("HeadLamp").light.enabled = false;
     batteries--;
     //GameObject.Find("GUI Text_batteryLife").guiText.text = batteryLife.ToString();
     
     if(words)
     {
         instruction = null;
     }
     else
         {
             instruction = "To Turn Flashlight on or off, press O";
         }
     //guiLifeOfBattery.enabled = false;
     //guiRemainingBatLife.enabled = false;
 
 }
 
 function Update () 
 {    
     if(GameObject.FindWithTag("HeadLamp").light.enabled == true)
     {
     
     
         
         //guiLifeOfBattery.enabled = true;
         //guiRemainingBatLife.enabled = true;
         instruction = null;
         
         
         
         batteryLife = batteryLife -(batteryReductionSpeed * Time.deltaTime);
         //GameObject.Find("GUI Text_batteryLife").guiText.text = batteryLife.ToString("#.");
         
         //guiLifeOfBattery.pixelOffset = Vector2(Screen.width * horizontalOffset02, Screen.height * verticalOffset02);
         //guiRemainingBatLife.pixelOffset = Vector2(Screen.width * horizontalOffset01, Screen.height * verticalOffset01);
         
     }
         if(Input.GetKeyDown("o") && !GameObject.FindWithTag("HeadLamp").light.enabled == true && batteries >= 0) 
         {
             if(batteryLife <= 0 && batteries > 0)
             {
                 batteries--;
                 batteryLife = 420;
                 lightRange = 100;
                 GameObject.FindWithTag("HeadLamp").light.range = lightRange;
                 
             }
             GameObject.FindWithTag("HeadLamp").light.enabled = true;
             
         }
         
         
         else if(Input.GetKeyDown("o") && GameObject.FindWithTag("HeadLamp").light.enabled == true || batteryLife == 0)
     {
         
         GameObject.FindWithTag("HeadLamp").light.enabled = false;
         instruction =    "To Turn Flashlight on or off, press O";
         
     }
     if(batteryLife <= 0)
         {
             batteryLife = 0;
             GameObject.FindWithTag("HeadLamp").light.enabled = false;
             instruction =    "To Turn Flashlight on or off, press O";
             
         }
         if(batteryLife <= 50)
         {
 
             GameObject.FindWithTag("HeadLamp").light.range = lightRange * batteryLife / 20;
             
             
                 if(batteryLife <= 25)
                 {
 
                     GameObject.FindWithTag("HeadLamp").light.range = lightRange * batteryLife / 20;
             
                 
                 }
         }
     
         
     
     }
     
     
     function OnGUI()
     {
                 
             
             GUI.Label (Rect (Screen.width /2 -100, Screen.height /6 + verticalOffset03, 210, 25), instruction);
             
             GUI.Label(Rect (Screen.width / 8 - horizontalOffset01, Screen.height /6 + verticalOffset03, 210,25), "Battery Life Remaining: ", batteryLife.ToString);
     }
     
     
     
 
              Answer by aldonaletto · Jun 17, 2013 at 12:59 AM
GUI.Label accepts only one string parameter, thus you must concatenate what you want to show in a single string. Furthermore, ToString is a function: ToString() displays the value in a default format, while ToString("F2") - for instance - displays the value with two decimals. Your code should be something like this:
 GUI.Label(Rect (...), "Battery Life Remaining: "+batteryLife.ToString());
 
               Another approach is to just concatenate the numeric value to a string - ToString() is internally called by the compiler in such cases:
 GUI.Label(Rect (...), "Battery Life Remaining: "+batteryLife);
 
              thanks. I was hitting all around it, but you hit the bullseye. thank you thank you. You don't happen to know how to do away with the decimal? It's not a big deal, but it would look better. by the way, which of the 2 approaches is the best. they both work, but one has to cost more?
If you want no decimals, just use ToString("F0"). I suppose that both approaches perform the same: if you don't explicitly use ToString, the compiler does it for you (actually, the concatenation operator converts numbers to strings).
Your answer
 
             Follow this Question
Related Questions
Multiple Cars not working 1 Answer
GUI problem 1 Answer
Gui label text change 0 Answers
c# Quit button wont quit game 1 Answer
Unity3D Game Time 1 Answer