Syntax, how do I limit unityscript from jumping to the end of my code?

so, I’ve created this code:

var firstTextField : String = "Type Your Answer Here";

var speakerText : String = "This is the first task; can you 'continue'?";

var correctAnswer : String = "continue";

	function OnGUI () {
		
//make a label x, y, width, height

		GUI.Label (Rect (Screen.width/2-100, Screen.height/2-70, 200, 100), "" + speakerText);
		
// Make a text field that modifies stringToEdit.
			
		firstTextField = GUI.TextField (Rect (Screen.width/2-150, Screen.height/2-10, 300, 20), firstTextField, 25);
	

		if (Input.GetKeyDown ("return") && firstTextField == correctAnswer)
	{
			print ("enter key was pressed");
			
			firstTextField = "";
			
			speakerText = "Good, now can you 'hop'?";
			
			correctAnswer = "hop";
	
	}	else if(Input.GetKeyDown ("return") && firstTextField !== correctAnswer) {
	
	print ("enter key was pressed");
			
			firstTextField = "";
			
			speakerText = "try again, now can you 'continue'?";
	
	}
	
	if (Input.GetKeyDown ("return") && firstTextField == correctAnswer)
	{
			print ("enter key was pressed");
			
			firstTextField = "";
			
			speakerText = "Good, now can you 'jump'?";
			
			correctAnswer = "jump";
	
	}	else if(Input.GetKeyDown ("return") && firstTextField !== correctAnswer) {
	
	print ("enter key was pressed");
			
			firstTextField = "";
			
			speakerText = "try again, now can you 'hop'?";
	
	}

	

}

what I want it to do is to show a “hint” then if you write the correct answer into the text field, and press enter, then the script moves on to the next “phase” by changing the “correctAnswer” variable, and the “speakerText” variable. but when I run the script, and type in the answer(or anything else for that matter) the script jumps all the way to the bottom and changes the variable “speakerText” to “try again, now can you ‘hop’?”.

So I’m wondering what I’m doing wrong, is it the “!=” operator I used? or is it the way I have formatted my if statements in line after each other, or is it the fact that I’m using the “function OnGUI” function; or is it something completely different? :slight_smile:

Thanks again for any help! :slight_smile: This must be like the fifth question or so I’ve posted today, and every time you guys have come through! :smiley:

I can’t really tell what you’re actually trying to accomplish, but I can show you why the result will always be "try again, now can you 'hop'?" and you should be able to figure out how to accomplish what you want.

Lets do some desk-checking for the OnGUI function:

  1. The text field gets rendered with the result stored in “firstTextField”
  2. We check whether or not the “enter” button has been pressed and whether we have the correct result in “firstTextField”. Assuming we pressed the enter button, in either case, we set “firstTextField” back to "".
  3. Then we check again for the “enter” button, which must be true, since we just assumed such.
  4. Now we check whether “firstTextField” is the correct answer. Since we just reset it, it really can’t be.
  5. Lastly we change the “speakerText” to “try again …”

I think you need to rethink how you are approaching the entire problem. You should also note that ever using the word “first” or a number in a variable hints towards a bad design decision. Either there is one of something, or a list/array of things. Having multiples of something is just asking for trouble.

Your input logic looks weird.

If you limit your logic to just checking the answer when enter is pressed it’ll get a lot simpler i think.

if (Input.GetKey (KeyCode.Return)){
  if (answerField == correctAnswer) {
    // print correct answer stuff here.  
}
else {
  // Print wrong answer stuff
}
}