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
0
Question by fogsam · Nov 17, 2011 at 01:28 PM · javascripttimerfloattypecastingtostring

Cannot convert 'String' to 'float (but im actualy converting a float to a string)

So im trying to convert my floating number to a String using the ToString() function, but im getting the error 'Cannot convert 'String' to 'float'', which is the opposite of what im trying to achieve.

What i am actually trying to do is to create a Lap Timer where it displays the Mins, seconds and hundredths of seconds but im way off this as i can even convert my data types from a float to a string which is rather annoying.

First post on here so go easy on me please :P

 function Update (){

 
 var ellapsedTime : float;
 ellapsedTime = Time.time - startTime;
 
 
 var sec = Mathf.RoundToInt(ellapsedTime % 60);

 var min:float = Mathf.Floor(ellapsedTime / 60);
 
 if(min < 10) {
 
        min = "0" + min.ToString();
 }
 
 if(sec < 10) {
 
     sec = "0" + Mathf.RoundToInt(sec).ToString();
 }
 
 guiText.text =  min + ":" + sec;
 

}

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

2 Replies

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

Answer by WillTAtl · Nov 17, 2011 at 01:38 PM

you declare the variable min as a float....

 var min:float = Mathf.Floor(ellapsedTime / 60);

And then you try to assign a string to it..

 min = "0" + min.ToString();

So that's why it's complaining. You do the same thing with sec, though the typing there is implied rather than explicit.

unityscript doesn't let you change a variable's type on the fly like this. You'll need to use a separate variable of type string to hold these results.

Comment
Add comment · Show 1 · 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 fogsam · Nov 17, 2011 at 01:52 PM 0
Share

thanks very much, that actually makes more sense to me now.

avatar image
0

Answer by syclamoth · Nov 17, 2011 at 01:47 PM

Well, the problem is pretty much as the title says! You shouldn't be trying to assign the result of a 'ToString()' function back to the variable which you are calling it on. Instead, you should create new variables of the correct type, which you use for the GUI.

These bits are correct-

 var sec : float = Mathf.RoundToInt(ellapsedTime % 60);
 
 var min : float = Mathf.Floor(ellapsedTime / 60);

These bits are bad (trying to put a string into a float-shaped hole)-

 min = "0" + min.ToString();
 sec = "0" + Mathf.RoundToInt(sec).ToString();

Instead, make new string variables to hold this information

 var minString : string = "0" + min.ToString();
 var secString : string = "0" + Mathf.RoundToInt(sec).ToString();

On a side note, why don't you use an integer for the 'minutes' value? I don't think you'll ever want to have decimal values there, so there's no reason not to.

Comment
Add comment · Show 1 · 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 fogsam · Nov 17, 2011 at 03:09 PM 0
Share

Hey so this is my finished code and it works just fine,

function Update () {

 ellapsedTime = Time.time - startTime;
 
 var tenth:int = $$anonymous$$athf.Floor(ellapsedTime *10)%10;
 var sec:int = $$anonymous$$athf.Floor(ellapsedTime % 60);
 var $$anonymous$$:int = $$anonymous$$athf.Floor(ellapsedTime / 60);
 
 var $$anonymous$$String:String = $$anonymous$$.ToString();
 var secString:String = sec.ToString();
 var tenthString:String = tenth.ToString();
 
 if($$anonymous$$ < 10) {
       $$anonymous$$String = "0" + $$anonymous$$String;
 }
 if(sec < 10) {
     secString = "0" + secString;
 }
 
 guiText.text =  $$anonymous$$String + ":" + secString + ":" + tenthString;
     

}

Simple enough and yeah i am now using int rather than float. Im rather new to this and im co$$anonymous$$g from AS3 where you can typecast toString when ever i like. But this makes sense to me now. thanks guys/girls

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Javascript int to float 1 Answer

How do I use current List Object [i] as the string name? 3 Answers

How to Typecast JS Variables as C# Classes? 0 Answers

Error: "ArgumentException: get_deltaTime can only be called from the main thread" 1 Answer

Timer Between Labels 2 Answers


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