How do you get OnTriggerEnter2D to activate only once?

I have an enemy script with OnTriggerEnter2D, very simple like so:

void OnTriggerEnter2D(Collider2D col)
    {
        if (col.CompareTag("Player"))
        {
            //do damage stuff
            DoKockback();
        }

DoKnockback is a function in another script that knocks the player back on taking damage. This happens instantly but my problem is even in the mere second it takes to knock the player back, OnTriggerEnter can trigger 2-3 times, applying damage to the player 2-3 times. I’d like the damage to only be applied once before getting knocked back. Any suggestions on how I can do this?

if i understand your question comparetag is like

CompareTag Is this game object tagged with tag ?

so player own objects also may hit the collisions. so you have to avoid that tag issue. use this

void OnTriggerEnter2D(Collider2d col)
{
     if(col.gameObject.tag("Player"))
    {
        //do ur stuff
        DoKnockBack();
     }
}

and OnTriggerEnter2D occurs only when hits. check rigidbody settings and tag settings too.

I’ve appeared to have fixed my issue by switching around the isTrigger on the colliders. Before the enemy collider was set to isTrigger and the player collider wasn’t. However, setting the player collider to isTrigger and disabling enemy isTrigger results in only one OnTriggerEnter hit. Thanks for your suggestions, guys.

I’ve had the situation where it would call multiple times per collision as well. A solution would be to use a bool to prevent it from firing more than needed.

 void OnTriggerEnter2D(Collider2D col)
 {
    if(col.gameObject.tag("Player"))
    {
        if (isColliding)
        {   //Prevention happens here. (Only for 1 frame (See Update()))
            return;
        }

        DoKnockBack();
        //Bool causing the prevention.
        isColliding = true;
     }
 }

void Update ()
{
    //Set isColliding to false on the next frame.
    isColliding = false;
}

What this does is basically allows the interaction to happen for the frame it ACTUALLY happens on. Sometimes you will get multiple calls per frame, and that is what we need to prevent. Allowing the Update method to set isColliding to false ensures you will be able to call a collision again. Hope this isn’t too confusing.