Search Unity

How do you correctly save changes to a scriptable object to disk?

Discussion in 'Editor & General Support' started by RakNet, Sep 9, 2020.

  1. RakNet

    RakNet

    Joined:
    Oct 9, 2013
    Posts:
    315
    I have code that creates a scriptable object and adds some data

    ScriptableObject
    Code (CSharp):
    1. public class AM4RepositoryScriptableObject : ScriptableObject
    2. {
    3.     public AM4Repository objectList;
    4. }
    5. [System.Serializable]
    6. public class AM4Repository
    7. {
    8.     [System.Serializable]
    9.     public class AM4RepositoryEntry
    10.     {
    11.         public UnityEngine.Object objectHeader;
    12.     }
    13.     public List<AM4RepositoryEntry> entries = new List<AM4RepositoryEntry>();
    14. }
    Code that adds to the ScriptableObject
    Code (CSharp):
    1. AM4RepositoryScriptableObject repository;
    2. repository = AssetDatabase.LoadAssetAtPath<AM4RepositoryScriptableObject>(assetPath);
    3. repository.objectList.AddEntry(existingEntry);
    4. EditorUtility.SetDirty(repository);
    5. PrefabUtility.RecordPrefabInstancePropertyModifications(repository);
    This works fine in a separate small project. However, in my main project, 80%+ of the time the file on disk is empty, even after hitting File / Save All

    1K is an empty file
    upload_2020-9-9_3-35-24.png

    For example, here is AvatarPortrait.asset on disk
    I can force saving by editing the Scriptable Object in the inspector
    For example, opening a text field, pressing spacebar then delete, then using File / Save All, the file gets written out to disk properly. You can see the file on disk now contains the information also in memory.

    Whether or not it fails doesn't seem to matter on the size or number of objects that I add. For example, I only have 4 very basic entries to add in "Interfaces" but the file on disk has no entries. However, WieldedArmaments has like 100 objects with meshes, and that file does have entries.

    I tried Undo.RecordObject before calling "repository.objectList.AddEntry(existingEntry);" However, that failed even in my simple test project, both not saving the data and causing the object to later revert itself when editing other stuff, as if I pressed Undo when I had not. I'm not sure if I was supposed to also use "EditorUtility.SetDirty(repository); PrefabUtility.RecordPrefabInstancePropertyModifications(repository);" but didn't look into it that carefully since the latter two calls worked where Undo.RecordObject just seemed to break things.

    Using Unity 2019.3.15f1.
    The same code works in my simple test project, without changes.
    My project is over 100 GB so I can't upload it to Unity support.

    Thanks in advance.