Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by Snowduck · Apr 21, 2016 at 08:12 AM · editorwindoweditorguipropertyfield

EditorGUILayout.PropertyField in Custom EditorWindow Shows Wrong Text On Focus

In a custom editor window, I have an EditorGUILayout.PropertyField showing one of two possible instances of a MonoBehaviour using a SerializedObject. The script that the PropertyField shows is controlled by a button that toggles one of the two scripts. The script only has one public string variable, Text.

When I open the window, I can see the property field (as a text field) and the toggle button, and pressing the button will make the text in the property field change like intended. But if I focus the text field and then press the toggle button, the text won't change. Then, if I click outside the window to unfocus it, the text suddenly changes. If I click back to the window to the text field, the text changes to the wrong value again. Basically, the field has the wrong text while it is focused until I change the text or click on another text field or other similar control (like a Game Object in the hierarchy or so).

Am I using the SerializedObject/SerializedProperty/PropertyField wrong? I tried logging the values of the SerializedProperty while the field is showing the wrong text, but it seems the SerializedProperty always holds the correct value.

Here's the code for my editor window:

 public class PropertyFieldEditorWindow : EditorWindow
 {
     Property property1 = null;
     Property property2 = null;
 
     bool toggle = true;
 
     [MenuItem("Window/Property Field Window")]
     public static void ShowWindow()
     {
         PropertyFieldEditorWindow window = GetWindow<PropertyFieldEditorWindow>("Item Database");
 
         GameObject gameObject1 = new GameObject("GameObject 1");
         Property firstProperty = gameObject1.AddComponent<Property>();
         firstProperty.Text = "First text!";
         window.property1 = firstProperty;
 
         GameObject gameObject2 = new GameObject("GameObject 2");
         Property secondProperty = gameObject2.AddComponent<Property>();
         secondProperty.Text = "Second text!";
         window.property2 = secondProperty;
     }
 
     private void OnGUI()
     {
         Property property = null;
 
         if (toggle)
             property = property1;
 
         else
             property = property2;
 
         SerializedObject serializedObject = new SerializedObject(property);
         SerializedProperty textProperty = serializedObject.FindProperty("Text");
         EditorGUILayout.PropertyField(textProperty);
 
         serializedObject.ApplyModifiedProperties();
 
         if (GUILayout.Button("Toggle"))
             toggle = !toggle;
     }
 }

And the Property script:

 public class Property : MonoBehaviour
 {
     public string Text = "";
 }

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Soraphis · Apr 21, 2016 at 10:00 AM

 if (GUILayout.Button("Toggle")) {
     GUI.FocusControl("");
     toggle = !toggle;
 }

this should solve your problem

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 Snowduck · Apr 21, 2016 at 12:30 PM 0
Share

Thanks, worked perfectly!

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Change look of one type to different everywhere 1 Answer

Can't See all content of Editor 0 Answers

How do I draw curves in a custom editor window? 1 Answer

SearchableEditorWindow 2 Answers

Custom Editor fields listed below script variables 1 Answer


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