Stop previous coroutine and start new one from beginning
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.
Answer by hoekkii · Jan 08, 2017 at 04:43 PM
I guess something like this, I would not recommend this structure. Maybe use a tween library or create your own, for easier aborting and stuff. Note I did not test this, but it should work.
static bool StopShowAmmoText;
static IEnumerator show_ammo_text(int ammo)
{
// Stop the other called enumerators and wait one frame
StopShowAmmoText = true;
yield return null;
StopShowAmmoText = false;
// Settings
const float WaitTime = 1.0f;
const float ShowTime = 0.5f;
const int Increments = 10;
Color newColor = new Color(UnityEngine.Random.value, UnityEngine.Random.value, UnityEngine.Random.value, 1f);
// Apply the text and color
ammo_text.enabled = true;
ammo_text.color = newColor;
ammo_text.text = "AMMO LEFT ~ " + ammo;
// Keep doing this wile the time this function is running is lower than (WaitTime + ShowTime)
float time = 0.0f;
while (time < (WaitTime + ShowTime))
{
// Raise the time
time += Time.deltaTime;
// Wait for WaitTime amount of second
if (time < WaitTime)
{
// Keep checking if we need to stop
if (StopShowAmmoText) { yield break; }
yield return null;
continue;
}
// Calculate the new color
float t = 1.0f - Mathf.InverseLerp(WaitTime, (WaitTime + ShowTime), time);
newColor.a = Mathf.Round(t * Increments) / Increments; // This one uses increments
// newColor.a = t; // Use this one for fully smooth
ammo_text.color = newColor;
// Check if we need to stop
if (StopShowAmmoText) { yield break; }
yield return null;
}
// We finished so disable the text
ammo_text.enabled = false;
}
Thank you! This is just what I needed. I've just copy-pasted this into my game to test it and it works. Only problem is the text waits, then dissapears, appears and then dissapears again. I'll take the time to understand what you wrote. I rewarded you. Have a good day!
No problem. I just added some comments, maybe it helps. Also probably solved the thing you described?? If you got any further questions or questions about the code, just ask. Thank you! You too have an awesome day!
Your answer
Follow this Question
Related Questions
Calling coroutine in a method issue. 1 Answer
Visible popup damage notifier 0 Answers
How to resize image to what's inside? 0 Answers
[SOLVED]My UI text don't update from script 3 Answers
Trying to move buttons via Coroutine and transform.Translate 0 Answers