x


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!!

more ▼

asked May 04 '10 at 11:12 AM

Ruben gravatar image

Ruben
50 7 9 11

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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.

more ▼

answered May 04 '10 at 04:53 PM

Ray Pendergraph gravatar image

Ray Pendergraph
1.6k 15 21 37

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?

May 05 '10 at 08:08 AM Ruben

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. Maybe store the TextField() results in a temp string and compare to the comments field after the call... this might be helpful.

May 05 '10 at 12:16 PM Ray Pendergraph

will try it thank you!

May 05 '10 at 02:44 PM Ruben

that worked awesomely, thanks!!

May 06 '10 at 08:14 AM Ruben
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x156
x58
x31

asked: May 04 '10 at 11:12 AM

Seen: 3313 times

Last Updated: May 04 '10 at 11:12 AM