gameObject.GetComponent("ScriptName");
如果發現要抓取GetComponent上的值為大寫可能就要注意一下原本的變數是否為小寫如圖1所示:
TextMesh是3D Text上原本就包含的Component,可以發現到他所有變數都為大寫,但使用上並不是這樣。
1.Unity會將開頭的英文字元自動轉換成大寫。例如 text > Text。
2.而駝峰式的命名法TextMesh會變成Text Mesh會讓人誤認為中間有空白字元。
圖1
圖2
var vaule:String;
var scriptName;
var getVaule:String;
function Start(){
vaule="You See Me";
}
function OnCollisionEnter(target : Collision){//當進入碰撞
Debug.Log("OnCollisionEnter");
//取得target(撞到的目標)上的Script名稱
scriptName=target.gameObject.GetComponent("MySetComponent");
Debug.Log("Script Name: "+scriptName);
//取得target(撞到的目標)上Script的值
getVaule=target.gameObject.GetComponent("MySetComponent").vaule;
Debug.Log("Get Vaule: "+getVaule);
}
function Update(){
if(transform.position.x<15)
transform.position+=Vector3.right*Time.deltaTime*5;
}
var vaule:String;
function Start(){
vaule="I Love EasyUnity !";
}
function OnCollisionExit(target : Collision){ //當離開碰撞
Debug.Log("OnCollisionExit");
//傳送target(撞到的目標)上Script的值
target.gameObject.GetComponent("MyGetComponent").vaule="EasyUnity";
Debug.Log("Target Vaule: "+target.gameObject.GetComponent("MyGetComponent").vaule);
}
結果


你好
回覆刪除我不太能體會其中意思...
本身使用C# 一時無法轉換
能否請版主使用C#進行解釋....
感謝萬分