- Home /
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;
}
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.
thanks very much, that actually makes more sense to me now.
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.
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