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 = ""; }
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 == "
"[0]) {
s = “”;
}
}