unity-history-window: UnityException: Creating asset at path Assets/Gemserk.SelectionHistory.asset failed.

UnityException: Creating asset at path Assets/Gemserk.SelectionHistory.asset failed.
Gemserk.SelectionHistoryReference..cctor () (at ./Library/PackageCache/com.gemserk.selectionhistory@1.1.16/Editor/SelectionHistoryReference.cs:25)
Rethrow as TypeInitializationException: The type initializer for 'Gemserk.SelectionHistoryReference' threw an exception.
Gemserk.SelectionHistoryWindowUtils.SelectionRecorder () (at ./Library/PackageCache/com.gemserk.selectionhistory@1.1.16/Editor/SelectionHistoryWindowUtils.cs:49)
UnityEditor.Selection.Internal_CallSelectionChanged () (at <53ddbed73faf4fe3b980a493ab4e6639>:0)

Not sure what to make of this. It happened on a full project reimport.

https://github.com/acoppes/unity-history-window/blob/3d2f3d551e91919639e39fd3a05239c23795f996/Assets/Gemserk.SelectionHistory/Editor/SelectionHistoryReference.cs#L25

Can’t see anything obviously wrong with the code…

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 16 (5 by maintainers)

Most upvoted comments

I think I have the solution for this btw. Unity has a mechanism for this exact pattern. ScriptableSingleton.

You can do this:

using UnityEngine;

namespace Gemserk
{
    [UnityEditor.InitializeOnLoad]
    [FilePath("Gemserk.SelectionHistory.asset", FilePathAttribute.Location.PreferencesFolder)]
    public static class SelectionHistoryAsset : ScriptableSingleton<SelectionHistoryAsset>
    {
        public SelectionHistory selectionHistory = new SelectionHistory();

#if UNITY_EDITOR
        private void OnEnable()
        {
            selectionHistory.OnNewEntryAdded += OnNewEntryAdded;
        }
        
        private void OnDisable()
        {
            selectionHistory.OnNewEntryAdded -= OnNewEntryAdded;
        }

        private void OnNewEntryAdded(SelectionHistory obj)
        {
             Save(true);
        }
#endif
    }
}

And simply remove the SelectionHistoryReference entirely.

This also has the advantage of putting the SO in your preferences folder, which (appropriately) takes it out of version control.

I haven’t tested it, but I’m assuming InitializeOnLoad will trigger the static instance initialization. If not then maybe this could work:

static SelectionHistoryAsset() {
  _ = instance;
}

Gets the instance of the Singleton. Unity creates the Singleton instance when this property is accessed for the first time. If you use the FilePathAttribute, then Unity loads the data on the first access as well. - https://docs.unity3d.com/2020.1/Documentation/ScriptReference/ScriptableSingleton_1-instance.html

I can open a PR if you’re keen.

Oh, ‘static field’ triggers some brain cells. I have domain reloading off for less pauses while developping. I’m used to recompile once at startup to initialize all static fields in my code. I couldn’t be bothered to fix that issue for now. I’ll get back to if I ever fix it 😄