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
}