I tried to add an Health System to my Unity Project, but i just don’t wont to work and i can’t fin anything in the internet.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerTakeDMG : MonoBehaviour
{
public int dmg;
public PlayerHealth playerHealth;
private void OnCollisionEnter2D(Collision2D collision)
{
if(gameObject.tag == "Player")
{
Debug.Log("Collsiion with "+ gameObject);
playerHealth.Damage(dmg);
}
}
}
using System.Collections;
using System.Collections.Generic;
using System.Security.Cryptography.X509Certificates;
using UnityEngine;
public class PlayerHealth : MonoBehaviour
{
public int maxHealth = 3;
public int currentHealth = 0;
public void Start()
{
currentHealth = maxHealth;
}
public void Damage(int dmg)
{
currentHealth -= dmg;
if (currentHealth <= 0)
{
Destroy(gameObject);
}
}
public void Heal (int heal)
{
currentHealth += heal;
if (currentHealth > 3)
{
currentHealth = maxHealth;
}
}
}
Those ar my two script
The Player

Enemy
At the moment I can walk on my enemy without any damage.
I hope someone can help me. I am realy new into unity and general coding.