This used to work fine, but when we updated to 2022.3.22f1.
We encountered an issue where if the script is located in the package.
The function below returns null.
AssetDatabase.LoadAssetAtPath<T>(assetPath)
even doing
var allAtPath = AssetDatabase.LoadAllAssetsAtPath(assetPath);
Returns all the assets correctly, and they are there, but if we try to cast it to the type T.
It just returns null.
Any advice or tips?
This feels like a bot response.
But yea everything is checked it just returns null still
It is a spam bot
Looks like they’ve figured out to use ChatGPT to feed people a seemingly genuine response, just to churn out their baiting URL.
1 Like
I seem to have found the issue.
Its because it happens during a CI, that there was an Importer(MonoImporter) generated inconsistent result for asset.
Is there any callback i can listen to, so that if this happens, i can reimport the package?
Late answer, but the original “is null” is the issue. You’re probably trying to load an asset either during a static ctor or in a InitializeOnLoad(Method) class or method. In both cases the AssetDatabase is off limits this early in the initialization process.
What you can do is to delay the affected operation (this will also fix the “inconsistent results” or “asset import worker” warning spam):
[InitializeOnLoadMethod]
static void OnLoad()
{
EditorApplication.delayCall += () => {
var asset = AssetDatabase.LoadAssetAtPath<T>(assetPath);
Debug.Log($"asset loaded: {asset}");
}
}
1 Like