Strings

I’m having issues with a simple little thing:

var f : float;
var s : String;
var f = parseFloat(s);

If s (which is input by the user) contains something other than a number an error occurs and Unity halts execution.

This isn’t what happens in JavaScript, and it’s really really annoying.

So:

  1. Is there documentation for how UnityScript String instances work somewhere?

  2. Any ideas on this specific problem beyond manually checking all the characters in s?

You need to catch the exception thrown by parseFloat():

var s : String;
var f : float;

function Start() {
	try {
		f = parseFloat(s);
	} catch (e) {
		Debug.Log("Couldn't parse s as a float");
	}	
}

not sure if this will help but there’s also inputString…

http://unity3d.com/Documentation/ScriptReference/Input-inputString.html

[edit: sorry - that really doesn’t address your q. just thought it may help with what you’re going for…]

Ah – exceptions work … better than what I was doing :slight_smile: