以下の内容はhttps://baba-s.hatenablog.com/entry/2024/03/13/143935より取得しました。


【Unity】ArgumentException: GetComponent requires that the requested component 'GameObject' derives from MonoBehaviour or Component or is an interface.

概要

using UnityEngine;

public class Example : MonoBehaviour
{
    private void Start()
    {
        var go = gameObject.GetComponent<GameObject>();
    }
}

上記のようなコードを実行したら

ArgumentException: GetComponent requires that the requested 
component 'GameObject' derives from MonoBehaviour or 
Component or is an interface.

この例外が発生する現象に遭遇した

using UnityEngine;

public class Example : MonoBehaviour
{
    private void Start()
    {
        var go = gameObject.GetComponent<Transform>();
    }
}

GetComponent で指定する型を Component を継承しているクラスに変更したら
正常に動作するようになった




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

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