Coroutine issues when working with color.a

So I am trying to make a sprite become less transparent over a period of time, and it becomes more visible once, but fails to do so again, resulting in the sprite being barely visible on the screen.
Whats supposed to happen is that the sprite keeps getting more visible every few moments until its alpha has reached 1.

This is my code:

public class ScreenFace : MonoBehaviour {

    public GameObject face;

	private void Start()
	{
        ColorAplhaChange(0);

        StartCoroutine ("show");
	}

	private void ColorAplhaChange(float a )
	{
        Color NewColor = face.GetComponent<SpriteRenderer>().color;
        NewColor.a = a; 
        face.GetComponent<SpriteRenderer>().color = NewColor;
	}



    IEnumerator show () {
        float b = 0.1f; 
        yield return new WaitForSeconds (b); 
        ColorAplhaChange(+0.01f); 
        yield return new WaitForSeconds (b); 
        ColorAplhaChange(+0.01f); 
        yield return new WaitForSeconds (b); 
        ColorAplhaChange(+0.01f); 
        yield return new WaitForSeconds (b); 
        ColorAplhaChange(+0.01f); 
        yield return new WaitForSeconds (b); 
        ColorAplhaChange(+0.01f); 
        yield return new WaitForSeconds (b); 
        ColorAplhaChange(+0.01f); 
        yield return new WaitForSeconds (b); 
        ColorAplhaChange(+0.01f); 
        yield return new WaitForSeconds (b); 
        ColorAplhaChange(+0.01f); 
        yield return new WaitForSeconds (b); 
        ColorAplhaChange(+0.01f); 
        yield return new WaitForSeconds (b); 
        ColorAplhaChange(+0.01f); 
        yield return new WaitForSeconds (b); 
    }
}

Anyone know whats going wrong?

Hi there!

So, I think the problem here is that your function, ColorAplhaChange (I assume you meant ‘Alpha’), takes in a new alpha value and sets the sprites alpha to that. This means that your coroutine, ‘show’, is simply setting the sprites alpha to 0.1f 10 times. That isn’t going to smoothly change it or anything. So, what you want to do is to modify the existing alpha by some amount to get to a new alpha and keep going like that. Here’s a nifty little coroutine I quickly threw together for ya that gets the job done. Give it a final alpha (the alpha you want it to go to) and a fade duration (how long it should take to fully fade):

private IEnumerator FadeSprite(float finalAlpha, float duration) {

        // Get some basic data
        Color initialColor = mySpriteRenderer.color;
        float elapsedTime = 0;
        float changeInAlpha = finalAlpha - mySpriteRenderer.color.a;

        // While there is still time left on the "timer"
        while (elapsedTime < duration) {
            // Set the sprite color
            float percentageComplete = (elapsedTime / duration);
            float newAlphaAdjustment = changeInAlpha * percentageComplete;
            mySpriteRenderer.color = new Color(initialColor.r, initialColor.g, initialColor.b, initialColor.a + newAlphaAdjustment);

            // Updated time passed
            elapsedTime += Time.deltaTime;
            yield return null;
        }

        // Makes sure that the alpha is exactly what you wanted
        mySpriteRenderer.color = new Color(initialColor.r, initialColor.g, initialColor.b, finalAlpha);
    }

Hope that helps!

Found that this also worked, but Stratosomes answer is more effichent probably, and less ugly to look at in code form.

public class ScreenFace : MonoBehaviour {

    public GameObject face;

	private void Start()
	{
        ColorAplhaChange(0);

        StartCoroutine ("show");
	}

	private void ColorAplhaChange(float a )
	{
        Color NewColor = face.GetComponent<SpriteRenderer>().color;
        NewColor.a = a; 
        face.GetComponent<SpriteRenderer>().color = NewColor;
	}

    public bool willdo; 

    IEnumerator show () {
        if (willdo == true)
        {
            float b = 5f;
            yield return new WaitForSeconds(b);
            ColorAplhaChange(face.GetComponent<SpriteRenderer>().color.a + 0.01f);
            yield return new WaitForSeconds(b);
            ColorAplhaChange(face.GetComponent<SpriteRenderer>().color.a + 0.01f);
            yield return new WaitForSeconds(b);
            ColorAplhaChange(face.GetComponent<SpriteRenderer>().color.a + 0.01f);
            yield return new WaitForSeconds(b);
            ColorAplhaChange(face.GetComponent<SpriteRenderer>().color.a + 0.01f);
            yield return new WaitForSeconds(b);
            ColorAplhaChange(face.GetComponent<SpriteRenderer>().color.a + 0.01f);
            yield return new WaitForSeconds(b);
            ColorAplhaChange(face.GetComponent<SpriteRenderer>().color.a + 0.01f);
            yield return new WaitForSeconds(b);
            ColorAplhaChange(face.GetComponent<SpriteRenderer>().color.a + 0.01f);
            yield return new WaitForSeconds(b);
            ColorAplhaChange(face.GetComponent<SpriteRenderer>().color.a + 0.01f);
            yield return new WaitForSeconds(b);
            ColorAplhaChange(face.GetComponent<SpriteRenderer>().color.a + 0.01f);
            yield return new WaitForSeconds(b);
            ColorAplhaChange(face.GetComponent<SpriteRenderer>().color.a + 0.01f);
            yield return new WaitForSeconds(b);
            ColorAplhaChange(face.GetComponent<SpriteRenderer>().color.a + 0.01f);
            yield return new WaitForSeconds(b);
            ColorAplhaChange(face.GetComponent<SpriteRenderer>().color.a + 0.01f);
            yield return new WaitForSeconds(b);
            ColorAplhaChange(face.GetComponent<SpriteRenderer>().color.a + 0.01f);
            yield return new WaitForSeconds(b);
            ColorAplhaChange(face.GetComponent<SpriteRenderer>().color.a + 0.01f);
            yield return new WaitForSeconds(b);
            ColorAplhaChange(face.GetComponent<SpriteRenderer>().color.a + 0.01f);
            yield return new WaitForSeconds(b);
            ColorAplhaChange(face.GetComponent<SpriteRenderer>().color.a + 0.01f);
            yield return new WaitForSeconds(b);
            ColorAplhaChange(face.GetComponent<SpriteRenderer>().color.a + 0.01f);
            yield return new WaitForSeconds(b);
        }
    }
}