I’m making a text for ammo to show up every time I shoot. Every time I shoot void update_ammo_text()
gets called.
The whole code looks like this.
public static void update_ammo_text(int ammo) {
Coroutine tempCoroutine = null;
if(tempCoroutine != null)
instance.StopCoroutine(tempCoroutine);
tempCoroutine = instance.StartCoroutine(show_ammo_text(ammo));
}
static IEnumerator show_ammo_text(int ammo) {
ammo_text.enabled = true;
Color newColor = new Color(Random.value, Random.value, Random.value, 1f);
ammo_text.color = newColor;
ammo_text.text = "AMMO LEFT ~ " + ammo;
yield return new WaitForSeconds(1f);
ammo_text.color = new Color(newColor.a, newColor.g, newColor.b, 0.9f);
yield return new WaitForSeconds(0.05f);
ammo_text.color = new Color(newColor.a, newColor.g, newColor.b, 0.8f);
yield return new WaitForSeconds(0.05f);
ammo_text.color = new Color(newColor.a, newColor.g, newColor.b, 0.7f);
yield return new WaitForSeconds(0.05f);
ammo_text.color = new Color(newColor.a, newColor.g, newColor.b, 0.6f);
yield return new WaitForSeconds(0.05f);
ammo_text.color = new Color(newColor.a, newColor.g, newColor.b, 0.5f);
yield return new WaitForSeconds(0.05f);
ammo_text.color = new Color(newColor.a, newColor.g, newColor.b, 0.4f);
yield return new WaitForSeconds(0.05f);
ammo_text.color = new Color(newColor.a, newColor.g, newColor.b, 0.3f);
yield return new WaitForSeconds(0.05f);
ammo_text.color = new Color(newColor.a, newColor.g, newColor.b, 0.2f);
yield return new WaitForSeconds(0.05f);
ammo_text.color = new Color(newColor.a, newColor.g, newColor.b, 0.1f);
yield return new WaitForSeconds(0.05f);
ammo_text.color = new Color(newColor.a, newColor.g, newColor.b, 0f);
ammo_text.enabled = false;
}
If I shoot and wait for the coroutine to finish, and shoot again it works as it should. But if I should with an automatic rifle (does not let coroutine finish first) the a
value in the color does not reset. It continues where it left off.