- Home /
This post has been wikified, any user with enough reputation can edit it.
Question by
shaydenharley · May 07, 2014 at 05:41 AM ·
gui-text
How to check value of TextField
Hey guys i am trying to make a simple command box and when the user types in "Exit" into the Text Field and they hit Enter i want the game to Exit
So far i have the below script but it doesn't exit the app.
private var stringToEdit : String = "Enter Code";
function Start () {
}
function OnGUI () {
if(GUI.enabled == true) {
stringToEdit = GUI.TextField (Rect (10, 10, 200, 20), stringToEdit, 25);
}
}
function Update () {
if (Input.GetKeyDown("enter"))
{
if(stringToEdit == "exit") {
Debug.Log("code correct");
Exit();
}
//do something
}
}
function Exit () {
Application.Quit();
}
Could anyone help me fix this because my scripting skills are very limited, thanks
Comment
Answer by maddFrogg · May 07, 2014 at 07:40 AM
You said you expect "Exit" and you are checking for "exit". The string comparison is case sensitive. Try this:
if(stringToEdit.ToLower() == "exit")
EDIT: Otherwise, if you want exactly the code "Exit", compare it properly.
Sorry, i missed that, but i changed it and it still didn't function