Whenever I hit the generator, its health will go down by 0.1 from 1.0 until it reaches 0. If it reaches 0, the generator will wait for 10 seconds before it regenerates its health by 0.5 per second until it fully regenerates by 1. My problem is that after it waits for 10 seconds it will regenerate its health until it reaches 1 right, but after it reaches 1, when I hit the generator again it will regenerate by 0.5 even though i didn’t asked it to regenerate yet. It will do that for 10 seconds until I can hit it properly again.
using UnityEngine;
using System.Collections;
public class HealthBar : MonoBehaviour {
public UnityEngine.UI.Image healthBar;
public float fillAmount;
public static bool generatorOff = false;
void Update () {
if (healthBar.fillAmount == 0) {
generatorOff = true;
}
if (generatorOff == true) {
StartCoroutine(GeneratorHealth());
}
}
void OnCollisionEnter (Collision collision) { //When character enters trigger
if(collision.collider.tag == "punch" && generatorOff == false) {
healthBar.fillAmount -= 0.1f;
}
if(collision.collider.tag == "punch2" && generatorOff == false) {
healthBar.fillAmount -= 0.1f;
}
}
IEnumerator GeneratorHealth () {
if (healthBar.fillAmount == 1) {
generatorOff = false;
} else {
yield return new WaitForSeconds (10);
healthBar.fillAmount += 0.5f * Time.deltaTime;
}
}
}