I want to make a image fade in then fade out when fuel reaches a certain amount.
In my update function i have
if (Fuel.value <= 20) {
StartCoroutine (LowFuelFadeInOut ());
}
Then the IEnumerator is
IEnumerator LowFuelFadeInOut ()
{
LowFuel.gameObject.SetActive (true);
//FADE IN
LowFuel.CrossFadeAlpha (1f, 1.5f, false);
yield return new WaitForSeconds (2f);
//FADE OUT
LowFuel.CrossFadeAlpha (0f, 1.5f, false);
yield return new WaitForSeconds (2f);
//FADE IN AGAIN
LowFuel.CrossFadeAlpha (1f, 1.5f, false);
yield return new WaitForSeconds (2f);
//FADE OUT AGAIN
LowFuel.CrossFadeAlpha (0f, 1.5f, false);
LowFuel.gameObject.SetActive (false);
}
The problem is that the IEnumerator is called again over and over every frame, so after the first fade in it tries to do the fade out and also the fade in again. So what happens to the image is that the image stays at the same alpha and slightly flickers.
Note: I need to be checking the fuel constantly or at least check the value each time it changes so taking the coroutine out of update (or similar) is not an option.
Does anyone have any ideas on what I could do so that the coroutine is only called once?