How to store a variable of the player

Hi everybody

I’m making a script to use for my cart system. This system exists out of a cart which moves on the same route thanks due a loop animation, and a trigger object attached to the car with this script:

static var destroyedobject : GameObject;

function Update(){
if(ClassSelectionScript.classselected == true && CartScript.seated == false){
destroyedobject = GameObject.FindWithTag("Player");
}
}

function OnTriggerEnter(otherObj: Collider){
if (otherObj.tag == "Player"){
Destroy(destroyedobject);
CartScript.seated = true;
}
}

The cart also has a script attached:

static var stopcommand : boolean = false;
static var seated : boolean = false;

function Update () {

if(seated == true){
stopcommand = true;
}
}

And the place the player leaves the cart, just a ordinary mesh with a trigger object, which has this script attached:

var instantiatedobject : GameObject;



function Update (){

if (CartTrigger.destroyedobject != null){

instantiatedobject = CartTrigger.destroyedobject;

}

}



function OnTriggerEnter(otherObj: Collider){

if (CartScript.stopcommand == true){

var instance : GameObject = Instantiate(instantiatedobject, transform.position+Vector3(1,0,0), transform.rotation);

CartScript.stopcommand = false;

CartScript.seated = false;

CartTrigger.destroyedbject = null;

}

}

What I’m trying to do is making the cart store the player and all the scripts and variables attached to it after destroying him, and instantiating him at the stopping point. The script destroys the player, but it doesn’t instantiate him when the cart enters the trigger. Does the script maybe reset the variable when the player is killed?

Please help me solve this problem!

It would be easier to unactivate the player (destroyedobject.active = false in your case). By the way, you should not use a Find method in update, it’s to expensive. Or at least, test if the variable is null before searching.

If you really want to destroy/instantiate, you need a reference to the prefab from the assets folder, not the instantiated object, because you are destroying it. And you can’t find this one with a Find function. Three (more two and a half really) ways, to affect it in the inspector manually, to access it from another object containing that prefab (which have been affected from the inspector so…) or with the resources folder and the function Resource.Load.