- Home /
Clearing TextArea / Text field
hi
i have a textfield in a gui. i have a bool to display the gui and activate the textfield, when i am done editing it and return to the window, my previous text is still in the window. how do i clear the textfield so when i open the gui again the textfield is empty?
thanks!!
Answer by Ray-Pendergraph · May 04, 2010 at 04:53 PM
The text field only displays what you call it with, it is stateless (for the most part). You need to clear the string that you are holding onto the edited value with so that its empty when you pass it in.
So looking at the documentation: http://unity3d.com/support/documentation/ScriptReference/GUI.TextField.html you would set stringToEdit = "";
when you wanted to clear the text field.
this is my code: newOrb.comments = GUI.TextArea (Rect (30, 250, 400, 100), newOrb.comments,300);
the comment field is initialized to empty string as you stated above, but it still holds the previous data. i switch the window on and of via bool value, is this a problem?
It's initialized to empty string but gets reassigned on every OnGUI depending on what is being typed in the text control. It need to be reset to "" whenever the boolean to display is set to false. That will not happen automatically. The GUI object does not store this type of state, so whatever is in newOrb.comments is what will get displayed. I would suggest putting some print lines to print out the value before and after the event that clears the comment. $$anonymous$$aybe store the TextField() results in a temp string and compare to the comments field after the call... this might be helpful.
Answer by intrepidolivia · Jul 28, 2019 at 04:41 AM
I know this is old, but I'm having this problem in Unity and the accepted answer is not helping me. Even when I have two entirely different string variables assigned to entirely separate text fields, the text is being carried over from one text field to the next when I disable one text field and display the other one.
screen.text = EditorGUILayout.TextField ("screen text:", screen.text, GUILayout.Height(64));
newScreenID = EditorGUILayout.TextField ("New screen ID:", newScreenID, null);
Does anyone have any additional input about this? Surely there must be some kind of string buffer in the GUI or this wouldn't be able to happen.