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