2013-09-25 85 views
0

那Unity3D js腳本錯誤,在PC上它做工精細的一員,但如果我嘗試切換平臺到Android ...BCE0019:「碰撞」不是「對象」

Trigger_Script:

#pragma strict 
var P : GameObject; 
var P_script; 
var P_objname = "Player"; 
var P_script_name = "Player_script"; 

function Start() { 

P = GameObject.Find(P_objname); 
P_script = P.GetComponent(P_script_name); 
} 

function OnTriggerEnter(collider : Collider) { 
if(collider.gameObject.name == P_objname) P_script.collision = true; 
} 

和Player_Script:

#pragma strict 
var collision : boolean; 
var respawn_position : Vector3; 
var respawn_time : int = 1; 
var respawn_objname = "Respawn"; 

function Update() { 
if(collision) { 
Respawn(); 
} 
    } 

function Respawn() { 
renderer.enabled = false; 
respawn_position = GameObject.Find(respawn_objname).transform.position; 
gameObject.transform.position = respawn_position; 
yield WaitForSeconds(respawn_time); 
renderer.enabled = true; 
collision = false; 
} 

幫我PLZ:O型

回答

0

如果你不知道類型提前,您應該使用SendMessage或者您想要使用polymorphism

如果你知道腳本類型的提前:

​​

如果知道腳本類型的提前:

//(Trigger script) 
#pragma strict 
var P_script : YourScript; 


function Start() { 
    var tmp = GameObject.Find(P_objname); 
    tmp.SendMessage("SetCollision",true); 

    P_script = tmp.GetComponent("YourScript"); 
    Debug.Log("Current collision: " + P_script.collision); 
} 

//(Player script) 
//... all your other code plus this: 
function SetCollision(var b : boolean){ 
    collision = b; 
} 
+0

我有測試這一點,但現在: NullReferenceException:未將對象引用設置爲對象的實例 Trigger_script.Start()(At Assets/Script/Trigger_script.js:8) #pragma strict var P_script:Player_script; var P_objname =「Player」; (){ function Start P_script = tmp.GetComponent(「Player_script」); P_script.collision = true; Debug.Log(「Current collision:」+ P_script.collision); } –

+0

@RaffaeleElShaarawyIabichino'tmp.GetComponent(「Player_script」);'必須*完全匹配您的腳本的名稱。嘗試使用'tmp.GetComponent(「Player_Script」);'(注意大寫'S') – Jerdak

+0

好吧我嘗試,並:NullReferenceException Player_script + $ Respawn $ 34 + $。MoveNext()(在Assets/Script/Player_script.js: 23) UnityEngine.MonoBehaviour:StartCoroutine_Auto(IEnumerator) Player_script:Update()(在Assets/Script/Player_script.js:13),另一個錯誤:NullReferenceException:對象引用未設置爲對象的實例 Trigger_script.Start ()(在Assets/Script/Trigger_script.js:9)。 LoL現在?幫我看看.... –