- Home /
OnCollisionEnter getting called more than once,OnCollisionEnter called more than once
So my player is moving forward and when it hits an obstacle, particle effect gets activated and i get debug.log sying particle active, everything works just fine except the script is getting called more than once when micro collision happends or my player hits two obstacles at the same time. I tried using a bool but i didnt seem to work, im really new at this so pls help :)
bool ObstacleHit = false;
public ParticleSystem crashEffect;
void OnCollisionEnter(Collision collisionInfo)
{
if (ObstacleHit == false) ;
if (collisionInfo.collider.tag == "Player")
{
{
ObstacleHit = true;
Debug.Log("Particle Active");
crashEffect.Play();
}
}
}
Answer by Cassos · May 01, 2020 at 10:27 AM
bool ObstacleHit = false;
public ParticleSystem crashEffect;
public void OnCollisionEnter(Collision colInfo)
{
if (ObstacleHit)
return;
if (collisionInfo.collider.gameObject.CompareTage("Player"))
{
ObstacleHit = true;
Debug.Log("Particle Active");
crashEffect.Play();
}
}
Try this out
well, nothing really changed, but i think the problem might be that my script is applied on the obstacle gameobject which is prefab, that might make problems with bool i think. But what can i do now ? I was trying to add this Bool variable on my player script ins$$anonymous$$d and tried to activate it in this script, however i dont know how to toggle my bool from player script in another script.