概要
using UnityEngine; public class Example : MonoBehaviour { private GameObject gameObject = new GameObject(); }
上記のように、メンバ変数の定義のタイミングで
new GameObject() を記述すると
Internal_CreateGameObject is not allowed to be called from a MonoBehaviour constructor (or instance field initializer), call it in Awake or Start instead.
このエラーが発生します
エラーを回避するには
using UnityEngine; public class Example : MonoBehaviour { private GameObject gameObject; private void Awake() { gameObject = new GameObject(); } }
new GameObject() の記述を Awake や Start に移動します