Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

EditorGUILayout.PropertyField does not seem to work?

Discussion in 'Scripting' started by StarManta, Oct 30, 2012.

  1. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,741
    So, I have created a custom editor for an overly complicated script, and put in multiple tabs full of PropertyFields. After creating them, I start trying to assign these properties and... lo and behold, they don't work! Nothing gets assigned!

    What gives? With no sample syntax, I can't be sure that I've gotten the syntax right, or if it's a Unity bug, or what, all I know is that it does not assign any of these references.

    Code (csharp):
    1.  
    2.     public override void OnInspectorGUI() {
    3.         InGameMenuController igm = target as InGameMenuController;
    4.         SerializedObject so = new SerializedObject(igm);
    5. .
    6. .
    7. .
    8.         EditorGUILayout.PropertyField(so.FindProperty("scoreNeedle"), true);
    9. .
    10. .
    11. .
    12.     }
    13.  
     
  2. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Check the SerializedObject documentation. It is essential to call its Update method at the beginning of each frame (in your example at line 4). And you have to call ApplyModifiedProperties at the end, meaning at line 8.
     
  3. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,741
    Thank you, works perfectly.
     
  4. greggman

    greggman

    Joined:
    Nov 30, 2013
    Posts:
    24
    What? The docs say absolutely nothing about how to use these functions AFAICT. Where did you discover the correct functions the call in the correct order at the correct times?
     
  5. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,194
    In the example code for the Editor class.

    Also this thread is three years old, but I can see the confusion. I saw a Unity dev write a post where he said that we'd get a "suggest improvement" button for manual pages. That'd be great.
     
  6. learner_CL

    learner_CL

    Joined:
    Dec 23, 2016
    Posts:
    6
    You can see ApplyModifiedProperties(); Function in the documentation