- Home /
Textfield returning two values simultaneously?
I'm trying to design a password system for loading levels, similar to Metroid. I'm using a Text Field to get the user's input, then comparing that input against a list of preset passwords when the user clicks a submit button. The issue is that the input being sent to the comparison is always an empty string.
When I put a Log call in OnGUI it looks like it is returning two frames of an empty string, and then one frame of whatever was actually in the field.
Code:
void submit(){
Debug.Log("Method call: " + password);
Application.LoadLevel(checkPassword(password));
}
int checkPassword(string passwordToCheck){
Debug.Log("String check: " + passwordToCheck);
int levelToLoad = 0;
switch (passwordToCheck){
case "Password1":
levelToLoad = 1;
break;
case "Password2":
levelToLoad = 2;
break;
...
}
Debug.Log(levelToLoad);
return levelToLoad;
}
void OnGUI(){
password = GUI.TextField (new Rect(Screen.width / 2 - 75, Screen.height / 2 - 38, 150, 75), password);
Debug.Log("From OnGUI: " + password);
}
OnGUI runs many times per frame - how are you triggering the call to submit?
Submit() is called when a GUITexture (a button that says "Submit") is tapped. The function to detect whether it was tapped uses HitTest and only activates when the Touch phase is "Began".
Do you have your class as top class of other ones?
I had a similar case and it was due to the call of the method being done from a subclass.
password is a global variable set to "" in Start. $$anonymous$$Issed it in the copy/paste.
There is no subclass that inherits this one, or vice versa.