AssetDatabase.LoadAssetAtPath with valid path still returning null

Hi All,

I have the following code to load an asset:

string[] _assetGUIDs = AssetDatabase.FindAssets("myAsset", new[] {"Assets/Editor/Data/Settings/" });
string _path = AssetDatabase.GUIDToAssetPath(_assetGUIDs[0]);
ScriptableObject _data = AssetDatabase.LoadAssetAtPath<ScriptableObject>(_path);

While the AssetDatabase.FindAssets query does return a assetGUID and the GUIDToAssetPath does return a path to a valid .asset file, the AssetDatabase.LoadAssetAtPath returns null.
This code used to work but after some reorganizing of folders in Unity (and ofcourse changing the path for the FindAssets method accordingly) the LoadAssetAtPath stopped returning a ScriptableObject.

Anyone an idea where to look for a solution? Project settings, Assembly references?

Thanks!

It matters what method you make these calls in. Some event methods such as InitializeOnLoad or static ctors cannot use certain methods because the AssetDatabase has not been fully initialized, thus returning null when you make these calls.

1 Like

Thank you for the reply. I found out what the issue was. While reorganizing the scriptable objects I tried to instantiate lost the reference to their script and therefore they couldn’t be loaded. The LoadAssetAtPath will then silently fail.