- Home /
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!
Why do you need to destroy and recreate like this? What are you trying to do?
I'm trying to simulate the player entering the cart and stepping out off it when the cart reaches a stopping point. And I think this is the only way to do it, because when I instantiate the prefab of the player all its variables are reset.
Answer by Berenger · Feb 14, 2012 at 05:07 PM
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.
.active doesn't work, it just freezes the player. I want the player to turn invisible and respawn at the stopping point. And I think one of the problems is triggering the Stoppingpoint Script. The animated cart doesn't seem to activate the trigger. How can I fix this?
the property active control all the component. when false, all components are disabled (the opposite isn't true though). The object can't be visible if active is false, unless the mesh renderer and the meshfilter are on an other objects in the hierarchy. So you need the root, and then use this function.
Thanks very much, now the player vanishes when he enters the trigger. But the cart still doesn't activate the trigger attached to the stopping point. I think the collider of the cart doesn't work, because the cart is animated. How can I make the collider work again while keeping the animation?
If the stopping point is at the last frame of the animation, you can use animation events.
Or, you can add a child with collider and rigidbody non-kinematic, without gravity and constrained on everything. That child will activate the trigger.