- Home /
OnTriggerEnter is running twice when collisions is detected. Couldnt fix.
void OnTriggerEnter(Collider Other) { if (!((gameObject.name == "Farmer") || (gameObject.name == "FoodProjectile"))) { if (gameObject.GetComponent().hunger == 1) { GameObject.Find("Farmer").GetComponent().score++; Destroy(gameObject); } else { int hungerTemp = gameObject.GetComponent().hunger; Debug.Log("hunger = " + hungerTemp.ToString()); hungerTemp--; gameObject.GetComponent().hunger = hungerTemp; }
if (Other.gameObject.name == "Farmer")
{
if (GameObject.Find("Farmer").GetComponent<PlayerController>().lives == 0)
Destroy(Other.gameObject);
else
GameObject.Find("Farmer").GetComponent<PlayerController>().lives--;
}
else
Destroy(Other.gameObject);
}
}
,
Answer by DenisIsDenis · Jun 05, 2021 at 01:38 AM
If there are several colliders on the object that falls into the Trigger Collider, then OnTriggerEnter can be called several times.
In this case, all colliders except one should be placed in child objects with different tags.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Best practice for checking which object has collided. 2 Answers
how to instantly check if two colliders overlaying? 2 Answers
Collision not detected 3 Answers