Premature level loading. String loads level, instead of Return key.

I’m trying to write a C# script, which works like PasswordField, but the users have to see what they are typing so I’m using TextField. The problem comes with submitting the string. First I had just the GUI.Button, which worked fine, but friend of mine said using Return key would be more intuitive. So now I’d like to have both.

When I type in the correct “password”, the script loads the next level (“signal1”) right away without me/the user hitting the Return key. (That’s why the “signalWrong” is a comment line).

It’s been a long time since I’ve been coding anything, so I have copied all of this KeyCode-stuff after trying to figure it out for couple of days. :stuck_out_tongue: I thought EventType.keyDown would solve the “self activating Return key”, but no. Any help is greatly appreciated.

using UnityEngine;
using System.Collections;

public class enterointi : MonoBehaviour
{

bool userHasHitReturn = false;
string stringToEdit = "";

void OnGUI()
{

    GUI.skin.textField.fontSize = 30;
    GUI.Button(new Rect(Screen.width / 2 + 14, Screen.height / 2 + 80, 40, 40), "");

    Event e = Event.current;
    if (e.type == EventType.keyDown && e.keyCode == KeyCode.Return);
    if (e.keyCode == KeyCode.Return) userHasHitReturn = true;
    else if (false == userHasHitReturn) stringToEdit = GUI.TextField(new Rect(Screen.width / 2 - 140, Screen.height / 2 + 80, 150, 40), stringToEdit, 20);

    if (stringToEdit == "119")
    {
        Application.LoadLevel("signal1");
    }
    else if (stringToEdit == "admin123")
    {
        Application.LoadLevel("signal1");
    }
    else
    {
     //   Application.LoadLevel("signalWrong");
    }

}

}

well, you don’t have GUI.Button wrapped in a n if at all, so not sure how you plan to use that to move on, next you don’t check useHasHitReturn before loading level. Finally, the lack of curly braces used for your if statements makes your code awfully difficult to read, which is pretty critical to getting people to help.

Here’s a rewrite of your code elements, tidied up a little:

void OnGUI(){
	GUI.skin.textField.fontSize = 30;
	userHasHitReturn = false;

	stringToEdit = GUI.TextField(
		new Rect(Screen.width / 2 - 140,
			Screen.height / 2 + 80,
			150,
			40),
			stringToEdit, 20);

	if(GUI.Button(new Rect(Screen.width / 2 + 14, Screen.height / 2 + 80, 40, 40), "")){
		userHasHitReturn = true;
	}

	Event e = Event.current;
	if(e.type == EventType.KeyDown && e.keyCode == KeyCode.Return){
		userHasHitReturn = true;
	}

	if(!userHasHitReturn) return;

	if(stringToEdit == "119"){
		Application.LoadLevel("signal1");
	}else if (stringToEdit == "admin123"){
		Application.LoadLevel("signal1");
	}else{
		Application.LoadLevel("signalWrong");
	}
}

Final note, there is a new UI system implemented in unity now, using Event Systems and callbacks, granted it may be over kill for your use case. However it is worth noting, as OnGUI is rather inefficient (called twice per frame!) and is more or less deprecated except for editor functionality.

Thank you so much Scribe!

Curly braces… Of course. Didn’t even realised them missing, because Unity didn’t mention them. Really sorry about the messy code. My first post here.

Thank you again. I think next thing I’ll do is update my Unity.