
空のオブジェクトにスクリプトをつけました。別のオブジェクトに新しいスクリプトをつけて、こちらに上のオブジェクトをアタッチしました。

このスクリプトで、コンポーネントを取得するときに、型引数にMonoBehaviourと入れてもはじめのスクリプトを取得できました。
- using UnityEngine;
-
- public class GetComponent : MonoBehaviour
- {
- [SerializeField] GameObject obj;
-
- // Start is called before the first frame update
- void Start()
- {
- var sc = obj.GetComponent<MonoBehaviour>();
- Debug.Log(sc.GetType());
- }
-
- }

スクリプトが複数あっても問題ありません。

- using UnityEngine;
-
- public class GetComponent : MonoBehaviour
- {
- [SerializeField] GameObject obj;
-
- // Start is called before the first frame update
- void Start()
- {
- var sc = obj.GetComponents<MonoBehaviour>();
- Debug.Log(sc[0].GetType());
- Debug.Log(sc[1].GetType());
- }
-
- }
