うにてぃブログ

主にUnityとC#に関する記事を書いていきます

【Unity】UnityException: LoadAssetAtPath is not allowed to be called during serialization, call it from OnEnable instead. Called from ScriptableObject

UnityException: LoadAssetAtPath is not allowed to be called during serialization, call it from OnEnable instead. Called from ScriptableObject

こちらのエラーは シリアライズされたクラスのコンストラクタで LoadAssetAtPath を呼び出せないとのことなので
別にメソッドを作ってそちらで呼び出せばエラーは出なくなります

[Serializable]
public class Sample
{
    public Sample()
    {
        // コンストラクタで LoadAssetAtPath をエラー
        AssetDatabase.LoadAssetAtPath();
    }
 
    public void Load()
    {
        // コンストラクタでは無いのでエラー出ない
        AssetDatabase.LoadAssetAtPath();        
    }
}