Converting string to float, or int (Preferably float)

My goal is to create a system where the user can enter a number (preferably even with decimals) into a GUI text box. The idea is that the string is converted to a float, and this float is used for other purposes…

Here is the code I am trying:

var a = "";
var b = 0;
a = GUI.TextField(Rect(10,120,40,20),a);
b = parseInt(a);

First of all, if the code worked, it would only give me an int - so is there is any other way of converting the string so that it returns a float?

Second of all, using this method, I get the following error:

Any help?

This works just fine:

var thestring : String;
var thefloat : float = 0.0;

function Start() {
	thestring = "1234.2";
}

function OnGUI () {
	thestring = GUI.TextField (Rect (10, 10, 200, 20), thestring, 25);
	thefloat = parseFloat(thestring);
	print(thefloat);
}

And please use the search as this comes up at least once a week or so. Searching the Scripting section only with the keyword “convert” turned up these two threads from last week, both of which discuss string-to-int and string-to-float techniques (and vice versa):

http://forum.unity3d.com/viewtopic.php?t=30012
http://forum.unity3d.com/viewtopic.php?t=30075

And yeah, parseFloat is the ticket. :slight_smile:

*Yes, that is the rally call for me to get the Scripting FAQ started and this is prime material!

Thanks!

It works perfectly! :smile:

I apologize, higgyB, I will try the search next time.