using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class playerHealth : MonoBehaviour {
public int maxHealth = 100;
public int currentHealth;
public Text HP;
public healthBar healthBar;
void Start () {
currentHealth = maxHealth;
healthBar.SetMaxHealth(maxHealth);
}
void Update () {
if (Input.GetKeyDown (KeyCode.H)) {
TakeDamage (10);
}
HP.text = currentHealth.ToString ();
}
public void TakeDamage (int damage){
currentHealth -= damage;
healthBar.SetHealth (currentHealth);
}
void OnCollisionEnter(Collision col){
if (col.gameObject.name == "Enemy") {
TakeDamage (10);
}
}
}
I tried this but It didn’t work.