OnTriggerEnter2d gets called twice sometimes.

Hi, I have a 2d game where every time my character hits a trigger collider I add a point. Most of the time it adds one point like its supposed to, but sometimes (about one in every ten or so) it will add two points. I only have one object with script on it, and have looked at other people’s solutions but I can’t figure it out. It seems to just be a unity problem to me. I have also tried debug logging and it logs twice whenever it adds two points. Any help would be much appreciated. Thanks. :slight_smile:

Hi everyone I fixed the problem by switching the collider on my player from a 2D polygon collider to a 2D circle collider. Interesting that the 2D polygon collider was having issues as it wasn’t very complex. But the 2D circle collider is working perfectly so I’m happy.

So, your character is moving through and if it hits an object with a collider, your character would call the Trigger function ?

I’d like to see your code please @jobo22

Well having seen your script, I have something to clarify.
First of all, why would you use the OnTriggerEnter2D event to set a variable to true and then on Update check if that variable is true to add score to your scoreboard? I suggest to do the following changes:

private void OnTriggerEnter2D(Collider2D collision)
{
    if (collision.tag == "Player")
    {
        Score.AddPoint();
    }
}

private void Update()
{
    //Use this normally, use the Score.AddPoint() event in the OnTriggerEnter2D event
}

That should solve your problem. I recommend you to read the Documentation related to OnTriggerEnter2D. You can find it here: Unity - Scripting API: MonoBehaviour.OnTriggerEnter2D(Collider2D)

I hope I’ve helped you! If not, let me know it!

do you have 2 objects with collision ?
maybe one on the player