コガネブログ

平日更新を目標に Unity や C#、Visual Studio、ReSharper などのゲーム開発アレコレを書いていきます

【Unity】InvalidOperationException: Could not execute the method because the containing type 'XXXX', is not fully instantiated.

概要

public static class Example<T>
{
#if UNITY_EDITOR
    [UnityEditor.InitializeOnEnterPlayMode]
    public static void InitializeOnEnterPlayMode()
    {
    }
#endif
}

ジェネリックな static クラスで InitializeOnEnterPlayMode を使用すると

InvalidOperationException: Could not execute the method because the containing type 'XXXX', is not fully instantiated.

上記の例外が発生します

using UnityEngine;

public static class Example<T>
{
    [RuntimeInitializeOnLoadMethod]
    private static void RuntimeInitializeOnLoadMethod()
    {
        Debug.Log( typeof( T ).Name );
    }
}

InitializeOnEnterPlayMode の代わりに
RuntimeInitializeOnLoadMethod を使用する場合は、
RuntimeInitializeOnLoadMethod を適用した関数は呼び出されません