Question by
Yunarii · Oct 12, 2018 at 03:24 AM ·
scripting problemhealthbar
Health script
Does anyone see an error here? I can't seem to make it work, bar appears but the fill doesn't change
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class EnemyHealth : MonoBehaviour {
public int startingHealth = 10; // The amount of health the player starts the game with.
public int currentHealth; // The current health the player has.
public Image healthBar; // Reference to the UI's health bar.
bool isDead; // Whether the player is dead.
bool damaged; // True when the player gets damaged.
void Start()
{
// Set the initial health of the player.
currentHealth = startingHealth;
healthBar = healthBar.GetComponent<Image>();
}
public void TakeDamage(int damage)
{
// Reduce the current health by the damage amount.
currentHealth -= damage;
healthBar.fillAmount = (currentHealth / startingHealth);
Debug.Log("Health : " + currentHealth);
}
// Update is called once per frame
void Update()
{
if (currentHealth <= (0))
{
Destroy(gameObject);
}
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612180206im_/https://answers.unity.com/themes/thub/images/avi.jpg)