Unity自學中…
Unity - Scripting API: Component.GetComponents
抓取複數個同名class,以陣列形式貯存

【GetComponent VS Component.GetComponent】
GetComponent returns only the first matching component found on the GameObject on which it is called, and the order that the components are checked is not defined.
前者只回傳最先抓取到的指定class,其他同名class會忽略,故需抓複數時用後者
Unity自學中…
假如於有繼承MonoBehaviour class的Script裡欲抓取其他元件,只要目標元件在同一件GameObject中,則可由GameObject.GetComponent<ComponentType>() 省略成GetComponent<ComponentType>()
Unity自學中…
AType atype;

void start()
{
atype = GetComponent<Atype>();
atype.DoSomething // DoSomeing 是貯存在Atype裡的method
}

透過抓取Atype再貯存結果至atype去呼叫aType裡的method
Unity自學中…
float a;
void start()
{
a = GetComponent<Atype>()._a ; // _a是貯存在Atype裡的公用浮點數,其值為1
Debug.Log(a) //結果為1
}
透過抓取Atype再貯存結果至a,去參考AType裡的_a進行各種計算應用 (改變a的值不會改變_a的值)
Unity自學中…
https://images.plurk.com/wrASWiF29A6vQN57uKUEL.png
https://images.plurk.com/5urygBNTLtasrOYju4MCbe.png
判斷名字以抓取指定GameObject中的Component (Script為Component的一種),並貯存至自定義變數,可使用Component中的公開變數
variable.variable defined in script that set as public
載入新的回覆