BCE0019:'collision'不是'Object'的成员

时间:2013-09-25 15:08:39

标签: android unity3d unityscript

这是Unity3D js脚本错误,在PC上工作正常,但如果我尝试将平台切换到android ...

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

1 个答案:

答案 0 :(得分:0)

如果您提前不知道P_script的类型,则应使用SendMessage,否则您将使用polymorphism

如果您提前知道脚本类型:

#pragma strict
var P_script : YourScript;


function Start() {
    var tmp = GameObject.Find(P_objname);
    P_script = tmp.GetComponent("YourScript");
    P_script.collision = true;
    Debug.Log("Current collision: " + P_script.collision);
}

如果您提前知道脚本类型:

//(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;
}