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();
}
public void SetMaxHealth(int maxHealth)
{
healthSlider.maxValue = maxHealth;
healthSlider.value = maxHealth;
}
public void SetHealth(int health)
{
healthSlider.value = health;
}
}