SerializedProperty doesn't update displayed value in Editor

I have a SerializedProperty which always has the correct value, including after an Undo, but in the Editor it displays the pre-Undo value.

In MyClass I have a public integer ‘width’, and I reference it in MyClassEditor as a SerializedProperty:

private MyClass script;
private SerializedObject serialScript;
private SerializedProperty width;

void OnEnable()    
	script = (MyClass)target;
	serialScript = new SerializedObject(targets);
	width = serialScript.FindProperty("width");

Then in OnInspectorGUI I create the slider in the Inspector

public override void OnInspectorGUI() 
  EditorGUILayout.PropertyField(width); //Show slider for width


  if ( Event.current.commandName == "UndoRedoPerformed")

Everything seems to work, and in the debugger I can confirm that after an Undo, ‘width’ reverts back to the correct value, but it just isn’t displayed in the Editor, it still displays the pre-Undo value.

How do I force the displayed value to update correctly?

(EDIT: Just noticed, clicking off the editor object in the Hierarchy, and then back on, updates it, so is it some kind of refresh problem?)


Ok, what was missing was a call to SerializedObject.Update() (so in my case serialScript.Update )

Note… this is different to any of the Editor or MonoBehaviour Update() methods, this seems specifically to refresh the serialization of the object in question.

I don’t know if it’s bad etiquette to provide your own answer, but in this case because of the lack of any documentation, I thought it might help someone struggling with Undo functionality.
All the info from the docs: “SerializedObject.Update() Update serialized object’s representation.” But it’s a bit of a chicken & egg situation, as it’s not mentioned unless you already know it exists to look it up :slight_smile: