- Home /
OnTriggerEnter strange bug
Hi.. I have a door with an animation. This animation starts automatically with the OnTriggerEnter. There's a car with an animation (I have done 4 prefab of this, So there are 4 same car) when the first car starts the door should open, instead It dosn't, when the second car starts, the door opens! When i make start the third car, the door doesn't open again, and with the fourth car the door works again.. ps: the door's box collider is on "trigger". This is really strange! someone can tell me why? Thank you in advance! Here's the script of the door:
#pragma strict
var Animazione = "PortaAutomatica";
var on : boolean;
var Audio1 : AudioClip;
function OnTriggerEnter (other : Collider) {
on = !on;
if (!on){
GetComponent.<Animation>()[Animazione].speed = 0.2;
GetComponent.<Animation>().Play(Animazione);
GetComponent.<AudioSource>().clip = Audio1;
GetComponent.<AudioSource>().Play();
}
if (on) {
}
}
Answer by hexagonius · Jan 28, 2016 at 10:07 PM
with each car you toggle the on bool. first time it's true, second time it's false, third time true, 4th time false. Remove the wrapper with the on Boolean.
Answer by Vinnie420 · Jan 31, 2016 at 12:53 AM
you initialize "on" as false, whenyou call: on = !on , you make it true, and since the code checks if it is false, the door will not open on the first car. you can solve this by removing on = !on (or place it below the if statements) or by changing if (!on){} to if(on)()