- Home /
OnTriggerEnter/Exit are activating twice
Hi, I am working on a simple FPS Game and the enemies dont shoot instead they try to touch you, and if they do you lose a life, pretty simple stuff, i do it using OnTriggerEnter to check if the player collides with something, but onTriggerEnter activates twice and this causes the player to lose two lives, it is not consistant sometime the player loses one life and other times he loses two, tried OnTriggerExit but still the same problem, if there is a way to work around it or a better way to do it, would appreciate any help.
Here is the code:
public class Collission : MonoBehaviour
{
public GameObject player;
public GameManager gameManager;
public Text livesText;
public int lives = 5;
private Vector3 ZeroPoint = new Vector3(0, 37, 0);
void OnTriggerEnter(Collider collisionInfo)
{
if (collisionInfo.GetComponent<Collider>().tag == "Enemy")
{
transform.position = ZeroPoint;
lives--;
}
}
void Update()
{
Debug.Log(lives);
livesText.text = lives.ToString();
if (lives <= 0)
{
//Finds Another Script And activates the EndGame function
//which Reloads The Scene.
FindObjectOfType<GameManager>().EndGame();
}
}
}
Are you sure each enemy has only one collider? Each collider will give you OnTriggerEnter message even if they are under the same Rigidbody
Answer by OnEd0t · Jul 22, 2019 at 02:37 PM
Um..Okay..this is kinda embarassing..turns out that the first pirson controller script is a collider in itself and i had both the first person controller script and the capsule collider on the player gameobject. sooooo... yeah thanks for the note.