GUITextField parsing error on simple game need help

Hi,

I make a simple Unity3D math game script that user must enter correct answer for the math problem given.
This is the script I used.
I just attached this scrip to an Empty game object on a blank scene.
Try this to figure out what I get.

#pragma strict
var a : int;					//Define variable a as int
var b : int;					//Define variable b as int
var c : int;					//Define variable c as int
var stringAnswer : String;			//Define the answer as string input
var numAnswer : int;				//Define answer in numeric as int

function Start()
{
	a = Random.Range(0, 9);		//Get random value for a
	b = Random.Range(0, 9);		//Get random value for b
	c = a + b;			// c = a + b
}

function OnGUI()
{
	GUI.Box(Rect(Screen.width/2 - 200, Screen.height/2 - 5, 400, 40), a + " + " + b + " = ");			//Create a box show the equation
	stringAnswer = GUI.TextField(Rect(Screen.width/2 + 30, Screen.height/2, 40, 30), stringAnswer, 2);		//Create a text field that player input the answer, wether it numeric or text.
	
	numAnswer = int.Parse(stringAnswer);				//Convert the string answer to int value
	
	if(numAnswer == c)						//Compare the int value of answer with c
	{
		print("Answer is Correct !!");				//If answer is equal c, tell in terminal that the answer is correct. In future will be a GUI text. 
	}
	else
	{
		print("Answer is Wrong !!");				//If answer is not equal c, tell in terminal that the answer is wrong. In future will be a GUI text. 
	}
}

then when I run, I get this error message :

FormatException: Input string was not in the correct format
System.Int32.Parse (System.String s)
test.OnGUI () (at Assets/test.js:20)

I use Unity3D v.3.5.5f3 on Windows7 64.

Are there someone could help me correcting this script?

Thank You very much for Your help.:smile:

you run your code in ongui what means that it is called every frame (probably several times each frame). this also means you convert an empty string to int (when game starts you have nothing entered) what throws the exception. encapsulate the logic in a condition fe when the user presses a button when finished (also required for numbers with more than one digit) or use tryconvert to avoid the exception. if you would have posted c# code i would have posted an improved version but i don’t touch unityscript code ;).

Thank’s for Your comment :slight_smile:

I modified the code to be:

#pragma strict
var a 	: int;
var b 	: int;
var c 	: int;
var d 	: String;																											; 
var answer : String;
private var stringAnswer : String;

function Start()
{
	a = Random.Range(0, 9);		//Get random value for a
	b = Random.Range(0, 9);		//Get random value for b
	c = a + b;
	d = c.ToString();
}

function OnGUI()
{ 
     GUI.Box(Rect(Screen.width/2 - 200, Screen.height/2 - 5, 400, 40), a + " + " + b + " = ");
     answer = GUI.TextArea(Rect(Screen.width/2 + 30, Screen.height/2, 40, 30), answer, 2);
     
     if(GUI.Button(Rect(Screen.width-200, Screen.height - 40, 40, 20), "OK"))
     {
          stringAnswer = answer;
          if(d == stringAnswer)
          {
          	print("Correct");
          }
          else
          {
          	print("wrong!!");
          }
     }
   
}

and the problem solved!!

Thank’s :smile: