拖拽法
public GameObject cube;//以托动的形式
void Start(){
//获取该物体身上的刚体组件
Rigidbody r=GetComponent<Rigidbody>();
}
挂载代码的地方就会出现一个Cube,这时候就需要将你想要获取组件的物体拖拽到该框框内。
标签法
通过物体的Tag值来获取物体身上的组件
private GameObject cube3;//以标签获取
void Start(){
cube3=GameObject.FindWithTag("Player");
Rigidbody r=cube3.GetCompoent<Rigidbody>();
}