コガネブログ

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

【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 を継承しているクラスに変更したら
正常に動作するようになった