x


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;

}

more ▼

asked Nov 17 '11 at 01:28 PM

fogsam gravatar image

fogsam
1 1 2 2

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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.

more ▼

answered Nov 17 '11 at 01:38 PM

WillTAtl gravatar image

WillTAtl
697 2 4 9

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

Nov 17 '11 at 01:52 PM fogsam
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Nov 17 '11 at 01:47 PM

syclamoth gravatar image

syclamoth
14.8k 7 15 80

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

function Update () {

ellapsedTime = Time.time - startTime;

var tenth:int = Mathf.Floor(ellapsedTime *10)%10;
var sec:int = Mathf.Floor(ellapsedTime % 60);
var min:int = Mathf.Floor(ellapsedTime / 60);

var minString:String = min.ToString();
var secString:String = sec.ToString();
var tenthString:String = tenth.ToString();

if(min < 10) {
    minString = "0" + minString;
}
if(sec < 10) {
    secString = "0" + secString;
}

guiText.text =  minString + ":" + secString + ":" + tenthString;

}

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

Nov 17 '11 at 03:09 PM fogsam
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3462
x350
x182
x30
x19

asked: Nov 17 '11 at 01:28 PM

Seen: 1353 times

Last Updated: Nov 17 '11 at 03:09 PM