Hey!
Im making a health system for a game but for some reason when i try to use coroutines to delay the time between losing health it doesnt work at all. If i put the same if statement into an update function then it works perfectly but it instantly drains the health.
public class BasicHealth : MonoBehaviour
{
static int maxHealth = 100;
static int currentHealth = maxHealth;
public GameObject healthGui;
public GameObject Player;
private void Start()
{
StartCoroutine("HealthLoss");
}
public IEnumerator HealthLoss()
{
if (GameObject.Find("Skeleton@Skin").GetComponent<FollowAndAttack>().isAttacking)
{
currentHealth = currentHealth - 10;
Debug.Log(currentHealth);
yield return new WaitForSeconds(1);
}
}
private void Update()
{
if (currentHealth <= 0)
{
SceneManager.LoadScene("menu");
}
}
private void OnGUI()
{
GUI.Label(new Rect(120, 25, 100, 20), currentHealth.ToString());
}
}