Damaging the player with a health bar when touching an enemy

I followed Brackeys tutorial on a health bar and now that I finished I tried so many different thing to get it to work but it is not working.

Player:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour
{
public int maxHealth = 15;
public int currentHealth;

public HealthBar healthBar;
void Start()
{
	   currentHealth = maxHealth;
	   healthBar.SetMaxHealth(maxHealth);
}

void Update()
{
	if (Input.GetKeyDown(KeyCode.Space))
	{
		TakeDamage(5);
	}
}

private void EnemyHit(Collider2D other)
{
   if (other.CompareTag("Enemy"))
   {
        TakeDamage(5);
    }
}

public void TakeDamage(int damage)
{
	currentHealth -= damage;

	healthBar.SetHealth(currentHealth);
}

}

Health Bar:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class HealthBar : MonoBehaviour
{
Slider healthSlider;

private void Start()
{
    healthSlider = GetComponent<Slider>();
}

public void SetMaxHealth(int maxHealth)
{
    healthSlider.maxValue = maxHealth;
    healthSlider.value = maxHealth;
}

public void SetHealth(int health)
{
    healthSlider.value = health;
}

}

Well i guess the concept that you did not get from the tutorial is the one of the OnCollision methods.

Instead you have this function:

 private void EnemyHit(Collider2D other)

which does the right thing, but if you add a Debug.Log("function EnemyHit has been called") you will see that this never is printed in the logs.

The reason for this is that whenever you have a rigidbody with a Collider attached collide with another collider unity will search for scripts on the GameObject of the Rigidbody and if there is a script it will check if there is a function called OnCollisionEnter (when 3D) or OnCollisionEnter2D (when 2D - obviously)

so all you probably have to do is rename EnemyHit to OnCollisionEnter2D. That should be it. Then you should see that if you add a Debug.Log to the function it is called.