WaitForSeconds. Completely lost with this

Hello, I’m a complete beginner and i’m having some problems with my code. I have a text on a canvas, on which I’m trying to put a timer. The problem, I’m using a coroutine to delay my code, but somehow it’s not doing what I wanted it to do. Can you help me?

public class Alberto : MonoBehaviour {

   public void contador(int i)
    {
        while (i <= 100)
            {
            gameObject.GetComponent<Text>().text = i.ToString();
            Debug.Log(i);
            i++;
            StartCoroutine(Wait());
        }
    }
    IEnumerator Wait()
    {
        yield return new WaitForSeconds(1);
    }

}

You didn’t say what you wanted this to do, so I’ll just assume you want it to display the time as an int? The problem here, is that CoRoutines are weird and don’t work that way. It takes some time to get used to them, but you can’t just throw a Coroutine somewhere and have it act like a “pause” as you are trying to do here. The thing about coroutines is that when you call StartCoroutine, absolutely nothing happens to the main block of code. What IS happening, is that you started a NEW block of code, and both of these run at the same time. If you tell the new block to wait for a second, it doesn’t make the first block wait, which is what you are trying to do. Tp achieve what you want here with code, this is what you’d need to do:

public void contador (int i)
{
    StartCoroutine(Wait(i));
}

IEnumerator Wait (int i)
{
    while (i <= 100)
    {
        gameObject.GetComponent<Text>().text = i.ToString();
        Debug.Log(i);
        i++
        yield return new WaitForSeconds(1);
    }
}

So as you can see here in this example, you’re starting your Coroutine and it is handling the timer and the WaitForSeconds. The most important part you need to understand here, is that when you start a Coroutine, it’s like you’re having two functions run at the same time, and the WaitForSeconds only makes the Coroutine wait, not the main code. If you made the main code wait, your ENTIRE GAME would wait for a whole second, Update() is run once every frame. If you made Update wait, there’d be no new frames until it finished.

Now that that’s out of the way, I’d like to recommend a few changes to your code, as it’s pretty inefficient. The biggest thing here, is that GetComponent<> is very costly to call, and you want to do it as little as possible. Something like that should be put in a start () function if possible. Also for your timet, there is already a built in timer that can be accessed by Time.time, meaning you don’t need a Coroutine for this. Here’s a more polished version that does what you need it to:

public class Alberto : MonoBehaviour
{
	Text txt;
	float startTime;
	bool doTimer;

	public void Start ()
	{
        txt = gameObject.GetComponent<Text> ();
        StartTimer ();
	}

	public void StartTimer ()
	{
		startTime = Time.time;
		doTimer = true;
	}

	public void StopTimer ()
	{
		doTimer = false;
	}

	void Update ()
	{
		if (doTimer)
			txt.text = Mathf.RoundToInt (Time.time-startTime)+"s";
	}
}

To start the timer, just call StartTimer (). For now I put that in Start () so the timer is active as soon as the scene is loaded. If you need to stop it, just call StopTimer ().