0

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

触发器脚本:

#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;
}

请帮帮我:O

4

1 回答 1

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;
}
于 2013-09-25T16:33:50.610 回答