Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

PropertyDrawer field is not updating

Discussion in 'Scripting' started by mrm83, Apr 22, 2020.

  1. mrm83

    mrm83

    Joined:
    Nov 29, 2014
    Posts:
    345
    From inspector, updating "property" value works fine.
    However, if I update "unit", it doesn't work. I am thinking it is because "unit" is a new serialized object and not the actual object from property?

    How can I make it so that "unit" is updatable?

    Code (CSharp):
    1. public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) {
    2.         EditorGUI.BeginProperty(position, label, property);
    3.         SerializedObject drop = new SerializedObject(property.objectReferenceValue);
    4.         SerializedProperty unit = drop.FindProperty("unit");
    5.  
    6.         // Don't make child fields be indented
    7.         var indent = EditorGUI.indentLevel;
    8.         EditorGUI.indentLevel = 0;
    9.  
    10.        
    11.         Rect rect = new Rect(position.x, position.y, 200, 25);
    12.         // Updating this value in inspector works
    13.         EditorGUI.PropertyField(rect, property, GUIContent.none);
    14.  
    15.         rect.x += rect.width;
    16.         rect.width = 250;
    17.         // Updating this value in inspector does not work
    18.         EditorGUI.PropertyField(rect, unit, GUIContent.none);
    19.  
    20.         EditorGUI.indentLevel = indent;
    21.  
    22.         EditorGUI.EndProperty();
    23.     }