Coroutine Not Continuing

Hi,
I am still in the process of learning unity and this is going to be my first full project. I have played around a little with unity before so I have some(just a little) idea about coroutines and I have previously created a script with a corountine and it works fine.
I just updated to El Capitan and created a new project in Unity. I created a 2d sprite in the editor and attached the following script to it - the intention being to fadeIn the sprite as soon at the scene starts.

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

public class FadeIn : MonoBehaviour {
	public SpriteRenderer _spriteRenderer;
	public float fadespeed = 2;

	private Color _originalColor;
	// Use this for initialization
	void Start () {
		
		_originalColor = _spriteRenderer.color;
		_spriteRenderer.color = new Color(_originalColor.r, _originalColor.g, _originalColor.b, 0.1f);
		StartCoroutine(fadeIn());
	}

	void onAwake()
	{
		
	}
	// Update is called once per frame
	void Update () {
	}

	IEnumerator fadeIn()
	{
		Debug.Log("alpha " + _spriteRenderer.color.a);
		_spriteRenderer.color = new Color(_originalColor.r, _originalColor.g, _originalColor.b, Mathf.Lerp(_spriteRenderer.color.a,1,Time.deltaTime * fadespeed));
		if(_spriteRenderer.color.a < 1.0f)
		{
			yield return new WaitForSeconds (0.1f);
		}
		else
			yield break;
	}
}

The scene has nothing else in it and there are no other scripts that have been created or attached to the sprite object.
The output on the console shows:
alpha 0.1

but that appears only once.
Any idea what I am doing wrong?

Coroutines don’t loop themselfs. They represent a chunk of code just like a normal method. It’s executed from top to bottom. Once it reaches the bottom the coroutine is finished / dead. If you want a piece of code to repeat you have to use a loop yourself.

The coroutine you had in mind would probably look like this:

IEnumerator FadeIn()
{
    Color c = _originalColor;
    c.a = 0f;
    while (c.a < 0.98f)
    {
        c.a = Mathf.Lerp(c.a, 1f, Time.deltaTime * fadespeed);
        _spriteRenderer.color = c;
        yield return null;
    }
    c.a = 1f;
    _spriteRenderer.color = c;
}

Note: I had to replace WaitForSeconds with null, otherwise the code doesn’t make much sense. Time.deltaTime only makes sense if you do something every frame. yield return null will wait for the next frame.

Also keep in mind that using Lerp that way doesn’t give you a linear fading. It will be very fast in the beginning and slows down at the end. Mathematically you can never reach the value 1f as in each step you move a little bit closer but the step size gets smaller and smaller. Eventually it will reach the value of 1.0f due to rounding errors but it will take a long time. That’s why i changed the condition to 0.98f and finally clamp the value to 1.0f;

If you actually want a linear fading you might want to simply use a for loop:

IEnumerator FadeIn()
{
    Color c = _originalColor;
    for (float t = 0; t < 1.0f; t += Time.deltaTime * fadespeed)
    {
        c.a = Mathf.Clamp01(t);
        _spriteRenderer.color = c;
        yield return null;
    }
}

Here we lerp “t” linearly from 0 to 1. if fadespeed is 1f it will take exactly 1 second to reach the end. If it’s 2f it will be twice as fast and only take 0.5 seconds.

This is another way to fadeIn with a coroutine (working in a project):

private IEnumerator FadeIn()
{
        float f = 255f;
        while (f > 0f)
        {

            f--;

            Color newColor = blinkingRend.material.color;
            newColor.a = f / 255f;

            if (newColor.a < 0)
                newColor.a = 0;

            blinkingRend.material.color = newColor;

            yield return new WaitForEndOfFrame();
        }
    }

EDIT: I was writing the code with speed control when the answer from @Bunny83 appeared. That’s the correct way ;))