Hi, I’m creating a custom editor window for a tool I’m developing. The “main asset” consist of a ScriptableObject called “Main Asset”, inside this asset are sub-assets or “elements”, also ScriptableObjects (added with the “AddObjectToAsset” method). The main asset looks like this:

In order to add a new element i create the instantiate of the ScriptableObject, add the asset to the main asset and then add the reference to a list. This is the code for the AddElement (an extension method for ScriptableObjects), this works fine with Undo/Redo (i think):
public static void AddElement<T>( this ScriptableObject scriptableObject , SerializedProperty listProperty , string name = "Element" , HideFlags hideFlags = HideFlags.None ) where T : ScriptableObject
{
if( !listProperty.isArray )
throw new System.Exception( "\"listProperty\" is not a List." );
T element = ScriptableObject.CreateInstance<T>();
element.name = name;
element.hideFlags = hideFlags;
string scriptableObjectPath = AssetDatabase.GetAssetPath( scriptableObject );
AssetDatabase.AddObjectToAsset( element , scriptableObjectPath );
AssetDatabase.SaveAssets();
Undo.RegisterCreatedObjectUndo( element , "Add element to ScriptableObject" );
listProperty.InsertArrayElementAtIndex( listProperty.arraySize );
SerializedProperty lastElement = listProperty.GetArrayElementAtIndex( listProperty.arraySize - 1 );
lastElement.objectReferenceValue = element;
}
The problem happens with the RemoveElement method, here when i run the “Undo” the asset is recovered just fine, but the reference to the list is still missing. I’m doing two things, first Destroying the subAsset, like the docs says with Undo.DestroyObjectImmediate, then recording the state of the list using Undo.RecordObject, these two methods are wrapped inside an Undo group. Here is the code:
public static void RemoveElement<T>( this ScriptableObject scriptableObject , int index , SerializedProperty listProperty ) where T : ScriptableObject
{
if( !listProperty.isArray )
throw new System.Exception( "\"listProperty\" is not a List." );
if( !Utilities.isBetween( index , 0 , listProperty.arraySize - 1 , true ) )
throw new System.Exception( "\"index\" out of range." );
if( listProperty.arraySize == 0 )
return;
SerializedProperty elementProperty = listProperty.GetArrayElementAtIndex( index );
//Undo
Undo.SetCurrentGroupName( "Remove element from ScriptableObject" );
int group = Undo.GetCurrentGroup();
Undo.DestroyObjectImmediate( elementProperty.objectReferenceValue );
Undo.RecordObject( listProperty.objectReferenceValue , "");
listProperty.DeleteArrayElementAtIndex( index );
listProperty.DeleteArrayElementAtIndex( index );
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
Undo.CollapseUndoOperations( group );
}
Here is a gif showing the problem:

So, what am i doing wrong? maybe i misunderstood something. I’m too green with Unity’s Undo functionality, any tooltip or suggestion is welcome.
Thanks in advance.
Edit: I forgot to mention, the line “Undo.RecordObject( listProperty.objectReferenceValue , “”);” gives me an error, i asume that’s because the list is not a “Unity.Object”.