2D Collision Detection

I’m working on a simple 2D game and I’m having trouble detecting collisions between two objects. I’m using rigid bodies and when I run the game, the collision detection does indeed work. But in my character script, I want to change the animation when that collision happens, but I don’t know how to call my collision function. Here’s my function:

void OnCollisionEnter2D(Collision2D col)
{
     if (col.gameObject.tag == "enemy") 
     {
		anim.SetBool ("isDead", true);
     }
}

I’m pretty sure the function is correct, but I don’t know how to use it basically. How/Where should I call that function if a collision with the enemies occurs?

Can you be more clear about the function you’re trying to call please? But if I am correctly understanding what you’re trying to say, here’s an example:

void OnCollisionEnter2D(Collision2D col)
 {
      if (col.gameObject.tag == "enemy") 
      {
         isDead(true);
      }
 }

void isDead(bool boolvalue)
{
     if (boolvalue)
     //Do something
}

This code, will call the isDead function and pass the argument true when the collider collides with another object with the tag of enemy.