Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by sinkler747 · Jun 17, 2013 at 12:48 AM · guiprogrammingtostring

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);
     }
     
     
     
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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);


Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sinkler747 · Jun 17, 2013 at 01:08 AM 0
Share

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?

avatar image aldonaletto · Jun 17, 2013 at 05:35 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges