Unity自學中…
@justfortrading
Mon, Jul 10, 2023 3:03 PM
【GetComponent】
Unity - Scripting API: GameObject.GetComponent
Unity語法篇-C#關於GetComponent(組件擷取)那檔事 - 巴哈姆特
Unity - GetComponent 教學
Unity自學中…
@justfortrading
Mon, Jul 10, 2023 3:09 PM
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自學中…
@justfortrading
Mon, Jul 10, 2023 3:16 PM
假如於有繼承MonoBehaviour class的Script裡欲抓取其他元件,
只要目標元件在同一件GameObject中
,則可由GameObject.GetComponent<ComponentType>() 省略成GetComponent<ComponentType>()
Unity自學中…
@justfortrading
Mon, Jul 10, 2023 3:20 PM
AType atype;
void start()
{
atype = GetComponent<Atype>();
atype.DoSomething // DoSomeing 是貯存在Atype裡的method
}
透過抓取Atype再貯存結果至atype去呼叫aType裡的method
Unity自學中…
@justfortrading
Mon, Jul 10, 2023 3:25 PM
float a;
void start()
{
a = GetComponent<Atype>()._a ; // _a是貯存在Atype裡的公用浮點數,其值為1
Debug.Log(a) //結果為1
}
透過抓取Atype再貯存結果至a,去參考AType裡的_a進行各種計算應用 (改變a的值不會改變_a的值)
Unity自學中…
@justfortrading
Tue, Jul 18, 2023 10:50 AM
判斷名字以抓取指定GameObject中的Component (Script為Component的一種),並貯存至自定義變數,可使用Component中的公開變數
variable.variable defined in script that set as public
載入新的回覆
【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會忽略,故需抓複數時用後者
void start()
{
atype = GetComponent<Atype>();
atype.DoSomething // DoSomeing 是貯存在Atype裡的method
}
透過抓取Atype再貯存結果至atype去呼叫aType裡的method
void start()
{
a = GetComponent<Atype>()._a ; // _a是貯存在Atype裡的公用浮點數,其值為1
Debug.Log(a) //結果為1
}
透過抓取Atype再貯存結果至a,去參考AType裡的_a進行各種計算應用 (改變a的值不會改變_a的值)
判斷名字以抓取指定GameObject中的Component (Script為Component的一種),並貯存至自定義變數,可使用Component中的公開變數
variable.variable defined in script that set as public