Preloaded Assets are only loaded properly during script changes or opening project settings.

Hello.

I’m using this script here from the docs as a test. I’ve added 3 ScriptableObjects to the “Preloaded Assets” list in Unity. When ever I open the project for the first time and hit play, only the first asset in the list is pre-loaded. I confirmed this by using a simple Debug.Log. I can also confirm this because the ScriptableObject instances are null except for the very first asset at index 0 at play time.

Here is what the console looks like when this happens.

Now the weird part is that if I make a change to a script or open the Project Settings window, then the the rest of the assets are loaded. Any asset that wasn’t pre-loaded will now be loaded and print to the console.

Here is what the console looks like after making a script change or opening project settings window.

After a script change or opening project settings, everything works fine and you can see by this log, the ScriptableObjects are loading as they should now when starting the game.

I have confirmed this is not project related by creating a new project and dropping the script into the project and repeating this steps. I’m using Unity 2019.3, but also tested on 2019.2 with same outcome.

Here is the script I’m using. Any ideas why this happens?

  public class WheelReferences : ScriptableObject
    {
       
        private static WheelReferences _instance;

        public static WheelReferences Instance
        {
            get { return _instance; }
        }

        private void OnEnable()
        {
            _instance = this;
            Debug.Log($"Instance for WheelReferences was set: {_instance!=null}");
        }
       
       
    #if UNITY_EDITOR
    [UnityEditor.MenuItem("Assets/Create/Wheel References")]
    public static void CreateAsset()
    {

        WheelReferences asset = ScriptableObject.CreateInstance<WheelReferences>();
        UnityEditor.AssetDatabase.CreateAsset(asset, "Assets/WheelReferences.asset");

        // Add the config asset to the build
        var preloadedAssets = UnityEditor.PlayerSettings.GetPreloadedAssets().ToList();
        preloadedAssets.Add(asset);
        UnityEditor.PlayerSettings.SetPreloadedAssets(preloadedAssets.ToArray());
    }
    #endif
    }
2 Likes

I have this problem too. I added a scriptableobject to the Preloaded Assets but OnEnable does not get called when I load up the editor. I’m using 2019.4.18f1

Hi, I’ve tried contacting Unity with bug reports but I haven’t gotten any responses. As a workaround I’ve been using this solution: https://medium.com/@fiftytwo/fast-singleton-approach-in-unity-fdba0b5309d5

It’s been working great for me. Give it a try.

I’ll consider that. Creating a static class that calls Resources.Load in the constructor also works

[InitializeOnLoad]
public static class Initializer
{
    private static readonly MyScriptableObject _myScriptableObject

    static Initializer()
    {
        if (!SessionState.GetBool("FirstInitDone", false))
        {
           _myScriptableObject = Resources.Load<MyScriptableObject>("MyScriptableObject");
           SessionState.SetBool("FirstInitDone", true);
        }
    }

The SessionState bool ensures that it only runs the first time the project is launched, rather than every single time the code is recompiled.