以下の内容はhttps://baba-s.hatenablog.com/entry/2023/01/02/000000より取得しました。


【Unity】UnityException: XXXX is not allowed to be called from a MonoBehaviour constructor

概要

UnityException: XXXX is not allowed to be called 
from a MonoBehaviour constructor (or instance field initializer), 
call it in Awake or Start instead. Called from MonoBehaviour 'YYYY'.

上記のようなエラーが発生した場合は

using UnityEngine;

public class Example : MonoBehaviour
{
    // ここでエラー
    public Material m_material = Resources.Load<Material>( "" );
}

エラーが発生している箇所の初期化処理を

using UnityEngine;

public class Example : MonoBehaviour
{
    public Material m_material;

    private void Start()
    {
        m_material = Resources.Load<Material>( "" );
    }
}

Awake や Start に移動するとエラーが発生しなくなる




以上の内容はhttps://baba-s.hatenablog.com/entry/2023/01/02/000000より取得しました。
このページはhttp://font.textar.tv/のウェブフォントを使用してます

不具合報告/要望等はこちらへお願いします。
モバイルやる夫Viewer Ver0.14