Unity GUI TextField in C#
11,445
Store the input variables as a member of your class/script, not the method. Each frame you reset it back to an empty string wiping out what the user was trying to input.
Note from the Unity3D documentation regarding the text
parameter:
Text to edit. The return value of this function should be assigned back to the string as shown in the example.
Try changing your code to:
//notice these are pulled out from the method and now attached to the script
string email = "";
string username = "";
string password = "";
string confirm = "";
void OnGUI () {
email = GUI.TextField (new Rect (250, 93, 250, 25), email, 40);
username = GUI.TextField ( new Rect (250, 125, 250, 25), username, 40);
password = GUI.PasswordField (new Rect (250, 157, 250, 25), password, "*"[0], 40);
confirm = GUI.PasswordField (new Rect (300, 189, 200, 25), confirm, "*"[0], 40);
if (GUI.Button (new Rect (300, 250, 100, 30), "Sign-up")) {
Debug.Log(email + " " + username + " " + password + " " + confirm);
}
}
Author by
Dannika Rodriguez
Updated on July 09, 2022Comments
-
Dannika Rodriguez almost 2 years
so I'm making a sign up scene in Unity, and when I used this script to put textfields and a button, but when I play I can't type in the textfield. What is wrong with my code?
void OnGUI () { string email = ""; string username = ""; string password = ""; string confirm = ""; email = GUI.TextField (new Rect (250, 93, 250, 25), email, 40); username = GUI.TextField ( new Rect (250, 125, 250, 25), username, 40); password = GUI.PasswordField (new Rect (250, 157, 250, 25), password, "*"[0], 40); confirm = GUI.PasswordField (new Rect (300, 189, 200, 25), confirm, "*"[0], 40); if (GUI.Button (new Rect (300, 250, 100, 30), "Sign-up")) { Debug.Log(email + " " + username + " " + password + " " + confirm); } }