Hello. I am trying to create a health bar, and when my enemy attacks, it goes down. But when I run it, once my mutant attacks, my health goes down instantly. I’m guessing it’s because of Update, but when I added a boolean it did the same things. If someone can help that would be much appreciated. Please keep in mind that I am new to Unity. Here is my code:`using System.Collections;
using UnityEngine;
using UnityEngine.UI;
public class CharacterHealth : MonoBehaviour
{
public float CurrentHealth { get; set; }
public float MaxHealth { get; set; }
public Slider healthBar;
public GameObject mutant;
public bool hasMutantAttacked = false;
void Start()
{
MaxHealth = 20f;
// Resets health to full on game load
CurrentHealth = MaxHealth;
healthBar.value = CalculateHealth();
}
void Update()
{
if(mutant.GetComponent<EnemyController>().MutantAudio.isPlaying && !hasMutantAttacked)
{
hasMutantAttacked = true;
}
if(hasMutantAttacked && mutant.GetComponent<EnemyController>().MutantAudio.isPlaying)
{
DealDamage(6);
hasMutantAttacked = false;
}
}
void DealDamage(float damageValue)
{
// Deduct the damage dealt from the character's health
CurrentHealth -= damageValue;
healthBar.value = CalculateHealth();
// If the character is out of health, die!
if (CurrentHealth <= 0)
Die();
}
float CalculateHealth()
{
return CurrentHealth / MaxHealth;
}
void Die()
{
CurrentHealth = 0;
Debug.Log("Dead");
}
}`
Thanks!