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.

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;
    }