Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Lance 1 · Oct 20, 2010 at 06:08 PM · textfieldeditorguilayout

TextField not clearing

Does anyone have an idea why this code would result in me having the string hang around in the text field:

EditorGUILayout.BeginHorizontal(); if (GUILayout.Button("+", GUILayout.Width(24.0f))) { if (m_CurEntry.m_Name.Length > 0) { SwitchSet.Switch newSwitch = new SwitchSet.Switch(); newSwitch.m_Name = m_CurEntry.m_Name; newSwitch.m_State = m_CurEntry.m_State; m_Set.m_Switches.Add(newSwitch);

             m_CurEntry.m_Name = "";
             m_CurEntry.m_State = false;
         }
     }
     m_CurEntry.m_Name = EditorGUILayout.TextField(m_CurEntry.m_Name, GUILayout.Width(100.0f));
     m_CurEntry.m_State = EditorGUILayout.Toggle(m_CurEntry.m_State);
     EditorGUILayout.EndHorizontal();

Thanks

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Lance 1 · Oct 20, 2010 at 07:03 PM 0
Share

Just discovered that if I enter text, hit enter, then hit my + button the field clears correctly. It seems the text stays around only if I type the text and then hit the + button without pressing enter first.

avatar image yoyo · Dec 31, 2010 at 12:57 AM 0
Share

I have a similar problem with text that is set procedurally not getting updated in the UI of my custom editor window. It's a filename in a text field, and if the cursor is in the text field when the user hits my browse-for-file button then the file they select is not displayed until user presses enter or clicks another control. Calling OnGUI() doesn't work, nor does Repaint(), nor Focus(), nor does setting GUI.changed = true.

3 Replies

· Add your reply
  • Sort: 
avatar image
8

Answer by drastick · Oct 29, 2012 at 08:36 AM

I was running into the same issue trying to clear a string in a TextField when I hit the clear button. The text would clear but the field would remain with the old text.

Here is my fix for to get it to clear, the trick is you need to set the next control name before FocusControl will work to force the text field out of focus.

 nameFilter = EditorGUILayout.TextField("Name", nameFilter);
 GUI.SetNextControlName("Clear");
 if(GUILayout.Button("Clear", GUILayout.MaxWidth(70.0f)))
 {
     nameFilter = "";
     GUI.FocusControl("Clear"); 
 }
 
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image $$anonymous$$ · Jun 07, 2017 at 11:32 PM 0
Share

Hello from 2017. Thanks, working like a charm!

avatar image
3

Answer by thienhaflash · Jan 19, 2013 at 11:16 AM

The textfield's content will only update when user input or change focus, so, you need to do it manually by setting GUIUtility.keyboardControl = 0;

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Munchy2007 · Jun 04, 2020 at 12:38 PM 1
Share

This is the simplest solution and probably ought to be the accepted answer.

avatar image
0

Answer by Bravini · Oct 20, 2010 at 07:03 PM

GUILayout works only on the OnGUI function that is drawn twice every frame. That is, if you want it to be shut down you need to code a condition for it to appear and switch it to off (an if statement will do the trick). http://unity3d.com/support/documentation/Components/GUI%20Scripting%20Guide.html

If your code is performance reliant I suggest you use GUIText instead, then your code would work on GUIText.enabled =false.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Lance 1 · Oct 20, 2010 at 07:08 PM 0
Share

I'm sorry - I should have clarified that I still want the control to remain visible, just the text to be cleared. I use the text field to allow for a new entry to be added, and the button takes what's in the field and adds it to a list and then clears the text field so it's ready for new text to be entered. I hope this is making sense and I'm not wasting anyone's time.

Thank you so much for your response!

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

EditorGUILayout textfield return value problem 1 Answer

Fix editor TextField cursor alignment 1 Answer

How can I determine when user presses enter in an editor text field? 3 Answers

EditorGUILayout.TextField returning empty string 3 Answers

How to update a EditorGUILayout Textfield value in realtime? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges