component及刚体rigidbody用法

关于getcomponent函数,rigidbody(2d)的嵌套关系及用法

1.getcomponent函数函数

在unity中脚本能够当作是可定义的组件咱们常常要访问同一对象或不一样对象中的脚本能够运用getcomponent<>来访问其余脚本,this

本例的另外两个脚本中都有公有变量,假设一个脚本名为anotherscript,另外一个为Yetanotherscript。咱们须要在usinganotherscriptspa

中访问这两个脚本。code

1 public  GameObject otherGameObject;
2 private Anotherscript anotherscript;
3 private YetAnotherScript yetanotherscript;
4 void Awake{
5 anotherscript=getcomponent<Anotherscript>;
6 yetanotherscript=otherGameObject.getcomponent<Yetanotherscript>;
7 }

在rigidbody中,一样能够使用此函数来绑定刚体对象component

1 public Rigidbody boat ;
2 void Start(){
3 boat=this.GetComponent<Rigidbody>();
4 }

此例先声明一个名为boat的变量,须要使用getcomponent方法来取得刚体组件的属性;对象

注: getcomponent方法对内存消耗较大,尽可能少用,而且须要在Start(),或者Awake()中调用。blog

相关文章
相关标签/搜索