Coroutine not running to the end

Hi all,

I am having a weird problem with a coroutine. Basically when I mouse over a sprite it fades in and out as long as the mouse pointer is over it, this works fine, but when the mouse exits the sprite I want the sprite to fade out until its alpha value reaches 0, to do so and because unlike the OnMouseOver function which is called every frame while the mouse is over the collider, I use a coroutine called in my OnMouseExit function.
The code below is what I use, but as soon as the mouse exits the sprite, the sprite’s alpha is set to 0 straight away, without fading out, I have no idea whey hence my post.
You will notice in the OnMouseExit function the last line is commented out, I have tried to call the coroutine using both methods, it gets called both times but the fade out does ot happen in either call.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Fader : MonoBehaviour {

	public float min_fade_speed;
	public float max_fade_speed;

	private SpriteRenderer sprite;
	private float fade_speed;

	// Use this for initialization
	void Start () {
		fade_speed = Random.Range (min_fade_speed, max_fade_speed);
		sprite = GetComponent<SpriteRenderer> ();
		reset_color ();
	}

	void reset_color() {
		//Initially transparent
		sprite.color = new Color(1, 1, 1, 0.0f);
	}

	// Update is called once per frame -- currently unused
	void Update () {
	}

	void FadeInAndOut() {
		sprite.color = new Color (1, 1, 1, Mathf.SmoothStep (0.0f, 1.0f, Mathf.PingPong(Time.time/fade_speed, 1f)));
	}

	IEnumerator FadeOut(float alpha_start) {
		Debug.Log ("Alpha is: " + alpha_start);
		while (sprite.color.a > 0.0f) {
			sprite.color = new Color (1, 1, 1, Mathf.SmoothStep (alpha_start, 0.0f, Time.time / fade_speed));
			yield return null;
		}
	}

	void onMouseEnter() {
		reset_color ();
	}

	void OnMouseOver() {
		FadeInAndOut ();
	}

	void OnMouseExit() {
		float alpha = sprite.color.a;
		Debug.Log ("Alpha is: " + alpha);
		StartCoroutine ("FadeOut", alpha);
		// StartCoroutine(FadeOut(alpha));
	}
}

Regards
Crouz

You might want to try something that also takes into account the current alpha value of your sprite.
Something along these lines:

private IEnumerator FadeIn() {
    var c = sprite.color;
    while ((1f - c.a) > 0.02f) {
        c.a = Mathf.Lerp(c.a, 1f, this.fadeInTime * Time.deltaTime); // example. put your preferred maths here
        sprite.color = c;
        yield return null;
    }
    c = sprite.color;
    c.a = 1f; // dead-set to final target value for clean exit state; is optional
    sprite.color = c;
}

Do that in reverse for fading stuff out.

You are using the time since start for your progress, not the time since fading begin. I assume fade_speed is the time in seconds for the fade.

IEnumerator FadeOut(float alpha_start) {
     Debug.Log ("Alpha is: " + alpha_start);
     float startTime = Time.time;
     while (sprite.color.a > 0.0f) {
         sprite.color = new Color (1, 1, 1, Mathf.SmoothStep (alpha_start, 0.0f, (Time.time - startTime) / fade_speed));
         yield return null;
     }
 }