Player attack doesn't recgonize different colliders (unity 2D)

I’m trying to create an enemy with two box colliders, so that if the player hits the left collider, the enemy gets knocked back to the right, and vice versa.
The player script is this:

public class PlayerAttack : MonoBehaviour {

private float timeBtwAttack;
public float startTimeBtwAttack;

public Transform attackPos;
public float attackRange;
public LayerMask whatIsEnemies;
public int damage;

void Update ()
{
    if (timeBtwAttack <= 0)
    {
        if (Input.GetKey(KeyCode.Space))
        {
            Collider2D[] enemiesToDamage = Physics2D.OverlapCircleAll(attackPos.position, attackRange, whatIsEnemies);
            for (int i = 0; i < enemiesToDamage.Length; i++)
            {
                enemiesToDamage*.GetComponent<Enemy>().TakeDamage(damage);*

}
timeBtwAttack = startTimeBtwAttack;
}
}
else
{
timeBtwAttack -= Time.deltaTime;
}

  • }*
    This is the enemy script:

public class Enemy : MonoBehaviour {
public int health;
public float speed;
public int knockBack;
Collider2D c;
public BoxCollider2D left;
public BoxCollider2D right;

  • void Update ()*
    {
    transform.Translate(Vector2.left * speed * Time.deltaTime);
  • }*
    public void TakeDamage(int damage)
    {
    health -= damage;
    Debug.Log(“damage taken”);
    if (c == left)
    {
    transform.position = new Vector2(transform.position.x + knockBack, transform.position.y);
    Debug.Log(“hit left side”);
    }
    if(c == right)
    {
    transform.position = new Vector2(transform.position.x - knockBack, transform.position.y);
    Debug.Log(“hit right side”);
    }
    if (health <= 0)
    {
    Destroy(gameObject);
    Debug.Log(“enemy is dead”);
    }
    }
    void OnCollisionEnter2D(Collision2D collision)
    {
    if(collision.gameObject.name == “Player”)
    {
    PlayerController.health–;
    Debug.Log(“player is hit”);
    }
    }
    When I play the game as it is now, the player can deal damage to the enemy when the space key is pressed, and the “damage taken” message appears, but the game just treats thw two colliders like one, and the position of the enemy doesn’t change.
    Any help would be appreciated ^^
    Note: player and enemy objects in comments

You are not telling the Enemy script which collider was hit. Remove the “c” Collider2D from the script and change the TakeDamage function to this:

public void TakeDamage(int damage, Collider2D c) {
    health -= damage;
    Debug.Log("damage taken");
    if (c == left) {
        transform.position = new Vector2(transform.position.x + knockBack, transform.position.y);
        Debug.Log("hit left side");
    }
    if (c == right) {
        transform.position = new Vector2(transform.position.x - knockBack, transform.position.y);
        Debug.Log("hit right side");
    }
    if (health <= 0) {
        Destroy(gameObject);
        Debug.Log("enemy is dead");
    }
}

Now, when you call the TakeDamage function, you need to provide which Collider2D was hit. In the PlayerAttack script, change it to:

void Update() {
    if (timeBtwAttack <= 0) {
        if (Input.GetKey(KeyCode.Space)) {
            Collider2D[] enemiesToDamage = Physics2D.OverlapCircleAll(attackPos.position, attackRange, whatIsEnemies);
            for (int i = 0; i < enemiesToDamage.Length; i++) {
                enemiesToDamage_.GetComponent<Enemy>().TakeDamage(damage, enemiesToDamage*);*_

}
timeBtwAttack = startTimeBtwAttack;
}
} else {
timeBtwAttack -= Time.deltaTime;
}
}