The Coroutine works properly, but the issue that I’m having is that the Coroutine doesn’t return to the Update function once it has finished. When I apply damage to my object for the first time, the health goes to 0, the object is set to inactive, the Coroutine starts, waits 3 seconds and then sets the object to active again.
After it resets to active though, once I apply damage to the object, the health doesn’t go down. I have tried numerous things to get it to work, and I’m just lost. Any help at all would be greatly appreciated!
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class EnemyHealth : MonoBehaviour {
public int health = 100;
public GameObject AI_Car;
public float sec = 3f;
// Use this for initialization
void Start ()
{
}
void Update()
{
if (health <= 0)
{
health = 0;
AI_Car.SetActive (false);
StartCoroutine (Spawn ());
}
}
void OnGUI()
{
GUI.Box (new Rect (10, 10, 100, 30), "HP | " + health);
}
public void applyDamage(int dmg)
{
health = health - dmg;
}
IEnumerator Spawn ()
{
yield return new WaitForSeconds (sec);
health = 100;
AI_Car.SetActive (true);
}
}