TextField does not show up

void OnGUI () {
GUILayout.BeginArea(new Rect(screenW / 4, 100, screenW / 2, screenH /
if(GUILayout.Button(“Change the Server’s IP”)){
//set a user-defined ip address for client to connect
//the default ip is ethernet jack B48 in the CSL lab.
Log.Debug(“reached”);
MyClient.strIP = GUI.TextField(new Rect(screenW/2 - 150, screenH/2 - 75, 300, 150), MyClient.strIP, 15);
}
GUILayout.EndArea();
}

The textfield does not show up after click the button. but console does print “reached” which indicates the button click event is handled. So whats wrong with my code then?

Thank you in advance!

2 Answers

2

You need to add a bool value.

right now your code draws the text field for a millisecond when you press the button.

You need something like this:

if(GUILayout.Button("Change the Server's IP")){
   ChangeServerIP = true
}
if(ChangeServerIP){
   MyClient.strIP = GUI.TextField(new Rect(screenW/2 - 150, screenH/2 - 75, 300, 150), MyClient.strIP, 15);
}

should work.

i did as you said. but the new problem is the textfield does not disappear after i hit enter. i tried to set the bool back to false after initiate textfield, but this will prevent textfield from showing up again!

Why - are you taking the button away? Otherwise clicking it should make it show up again.

Change ChangeServerIP = true to ChangeServerIP != ChangeServerIP This is make it so it can turn the textfield on and off.

You need to use GUILayout.TextField(MyClient.strIP) - the GUI.TextField is for use without a GUILayout.

… And use the boolean as suggested above.