- Home /
Clear gui Text field
I have a problem with a GUI test i've been doing. i'm trying to clear a Text field when the player hits a key. i have all the inputs set up and everything should work ,but i cant get it too. Here's the code i've been using to try to do this:` var string:String; function OnGUI () {
string = GUI.TextField (Rect (100, 135, 200, 20), string , 25); } function Update() { if (Input.GetButtonDown("enter")) { string = ""; } `
Answer by Eric5h5 · Apr 12, 2012 at 03:31 AM
Don't try to mix Input with OnGUI code.
var s : String;
function OnGUI () {
var returnHit = (Event.current.type == EventType.KeyDown && Event.current.keyCode == KeyCode.Return);
s = GUI.TextField (Rect (100, 135, 200, 20), s, 25);
if (returnHit) {
s = "";
}
}
That can be made a little shorter by using the actual character instead (since TextField eats the KeyCode.Return event it has to be checked first):
var s : String;
function OnGUI () {
s = GUI.TextField (Rect (100, 135, 200, 20), s, 25);
if (Event.current.type == EventType.KeyDown && Event.current.character == "\n"[0]) {
s = "";
}
}
Your answer
Follow this Question
Related Questions
Problem with Android Keyboard/TextField 1 Answer
Background of GUI.TextField disappears when using a GUIStyle 2 Answers
TextField not getting focus on iOS 1 Answer
GUI.TextField problem, won't update var. 0 Answers
gui text and password field 1 Answer