My Enemy script activates a raycast and line renderer when within range of the player. However using the Health script below my health slider continues to decrease in value after my enemy is destroyed or I move the player out of range.
New to unity, hope this is not too simple. I guess its cause there is nothing after CurrentHealth -= Time.deltaTime * 10; to stop the health decreasing but how would I fix this?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Health : MonoBehaviour
{
public Text HealthText;
public Slider HealthSlider;
public float MaxHealth = 100;
static public float CurrentHealth = 100;
public LineRenderer lineRenderer;
private void Update()
{
HealthUI();
if (CurrentHealth > 0)
{
if (lineRenderer.enabled)
{
CurrentHealth -= Time.deltaTime * 10;
}
}
}
public void HealthUI()
{
HealthSlider.value = CurrentHealth / MaxHealth;
HealthText.text = "HEALTH " + ((int)(CurrentHealth / MaxHealth * 100)).ToString() + "%";
}
}
Thanks!
static?if(CurrentHealth > 0)should prevent it from decreasing .. maybe it will not exactly terminate at0but a bit below but than it should stop .. at least from the code you provide ... Are you sure nothing else is changing the value? Especially since it isstaticit might be easily changed by another component - derHugo