- Home /
How do you change a variable in a script, that is on an object instantiated from a prefab?
I'm making a 2d game where the player drives down three lanes at an increasing speed while dodging other cars. When you crash in to another car I want both cars to stop. My problem is that if I just set the cars speed to 0, all the cars stop.
Here is the script on the player:
var godMode = false;
var deadCar : Animator;
static var carCollision = false;
deadCar = GetComponent(Animator);
function OnTriggerEnter2D(other: Collider2D) {
if (!godMode) {
//The cars collide
forwardmover.speed = 0;
deadCar.SetTrigger("Dead");
carCollision = true;
};
}
Here is the script on the other cars:
static var carSpeed : float = 5;
function Update () {
transform.Translate(transform.up * carSpeed * Time.deltaTime);
}
As I said I want to change the "carSpeed" variable to 0 on the car, I collide with. Any help is very much apreciated
static variables make my eyeballs bleed. Don't use static variables, it is the lazy way out of learning to use GetComponent and caching references to components. public variables and GetComponent is the answer to your question.
static variables make my eyeballs bleed. Don't use static variables, it is the lazy way out of learning to use GetComponent and caching references to components. public variables and GetComponent is the answer to your question.
Thanks for the answer. I don't know how public variables and GetComponent work. Can you tell me how I would implement them here?