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. 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");
}
}
}