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 :stuck_out_tongue:

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;

}

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.

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.