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. :-P 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");
}
}
}
Answer by Scribe · Apr 21, 2016 at 06:45 PM
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.
Answer by wolfncrow · Apr 22, 2016 at 12:44 PM
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.
No problem, you don't need curly braces, however it means only the 'line' directly afterwards is effected, see line 21 of my answer. Ending an if statement line with a semi-colon ';' will mean it doesn't fire at all, as with your line 11. In general always best to use curly braces, unless the function is obvious as with line 21 of my answer again. A few other notes, you should not use the new answer section to comment on things, rather, as I have done, use add comment at the bottom of what you wish to reply to.
Finally, if your question is answered, you can click 'Accept' at the bottom of the answer you wish to accept, that way others can find answers to similar questions more easily! (and I get karma :3). Have fun coding!
Your answer
Follow this Question
Related Questions
Convert keycode to string 1 Answer
Accessing a non-static string variable in another script C# 1 Answer
Accesing specific int from inspector 2 Answers
How do I save separate keycodes presses at same time and store for use later in update? 0 Answers
I'd like my object to be active only when space key is NOT pressed 1 Answer