Unity 5.1.1f1: Redo operation in custom editor causes some sort of corruption.

I’m trying to implement undo/ redo operations into my custom editor.
Essentially, the editor has a reference to a monobehaviour, and it directly edits this, and does Undo.RecordObject on it before changing things.

So my problem starts when i redo something that I’ve undone.
My monobehaviour has a list of a certain serializable object, and those objects have another list of another serializable object.
When i use RemoveAt on the first list (i record the undo directly beforehand), it works fine, and it allows me to undo it. But then when i redo the change, some of the data in a different list ID changes.
For example, if i use removeat on object.list1[0]
then some of the data in object.list1[5] gets corrupted after i undo then redo.
Specifically, the data in object.list1[5].list2[#] (But not all of the data)
The size of the lists stay the same, but the data in the list2 objects get reset to 0 or “”

If i run my custom editor with a reference to a prefab monobehaviour, it works fine.
It’s just when i run the custom editor with a reference to a monobehaviour that is currently in the scene.

All the data in the object seems to get correctly serialized. (If i change something in the editor, then save and reload unity, it sicks. And applying and reverting the object to the prefab also works correctly)

So it seems as if unity doesn’t have an up to date serialization of my monobehaviour before the undo is stored, and then it gets mucked up when the redo occurs. Although, because only some of the data gets corrupted, that might be unlikely.
Also, it seems that only items in list2 get corrupted when their list ID is greater than 2.

This is my first time doing custom editors and recording object changes, so hopefully i didn’t miss something completely obvious…

If code is needed, i will try to create a minimal code example when i get time.

tl;dr

  • Redo breaks scene object but not prefab object.
  • Deleting and item from a list is what causes the redo/ undo to go weird.
  • The items within other elements of that list get corrupted after the redo.

I suppose the correct answer to this is to use a SerializedObject to make the changes.
The first time i researched it, i didn’t realise that you could go deeper into the properties by doing “FindPropertyRelative”

I did find a fix for using my previous method, although it’s probably bad practice, but this seemed to work:

Undo.RecordObject(dialogue, "Reset Dialogue");
EditorUtility.SetDirty(dialogue);
//Changes here

This stopped the corruption. Although now i’m using this method and serialised objects at the same time, so i will probably slowly convert it all to serialised objects.