![](https://www.ame-name.com/wp-content/uploads/2020/12/WS011477-1.jpg)
空のオブジェクトにスクリプトをつけました。別のオブジェクトに新しいスクリプトをつけて、こちらに上のオブジェクトをアタッチしました。
![](https://www.ame-name.com/wp-content/uploads/2020/12/WS011478-2.jpg)
このスクリプトで、コンポーネントを取得するときに、型引数に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());
}
}
![](https://www.ame-name.com/wp-content/uploads/2020/12/WS011476-1.jpg)
スクリプトが複数あっても問題ありません。
![](https://www.ame-name.com/wp-content/uploads/2020/12/WS011480-1.jpg)
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());
}
}
![](https://www.ame-name.com/wp-content/uploads/2020/12/WS011479.jpg)