Using Wait for Seconds to countdown

I have the following code:

    public GameObject three;
public GameObject two;
public GameObject one;
public GameObject go;

IEnumerator wait()
{
	yield return new WaitForSeconds(1.0f);
}
void countDown() 
{
	three.SetActive(true);
	StartCoroutine(wait());
	three.SetActive(false);

	two.SetActive(true);
	StartCoroutine(wait());
	two.SetActive(false);

	one.SetActive(true);
	StartCoroutine(wait());
	one.SetActive(false);

	go.SetActive(true);
	StartCoroutine(wait());
	go.SetActive(false);
}
void Start()
{
	countDown();
}

But it is not cycling between the shapes as it should, it just happens instantly, any help appreciated!

That’s because the wait() coroutine it’s another thread of execution than countDown().

When you start, countDown() is called (thread 1), and eventually it calls wait() (thread 2). Because there are two separate threads of execution, countDown() doesn’t wait for wait() to finish to continue. In fact, if you put something like Debug.Log("Test"); after the WaitForSeconds() order, you will see that every time you call wait() it prints Test after a second.

The code should be like this:

void Start()
{
     StartCoroutine("CountDown");
}

IEnumerator CountDown()
{
     three.SetActive(true);
     yield return new WaitForSeconds(1.0f);
     three.SetActive(false);
     two.SetActive(true);
     yield return new WaitForSeconds(1.0f);
     two.SetActive(false);
     one.SetActive(true);
     yield return new WaitForSeconds(1.0f);
     one.SetActive(false);
     go.SetActive(true);
     yield return new WaitForSeconds(1.0f);
     go.SetActive(false);
}

So the CountDown() coroutine runs on another thread than the rest of the game, letting it to run without stopping each time we WaitForSeconds(1.0f) and returning each second for changing the active GameObject.

NOTE: Please note that I talked about threads for comprehensive explanation, but at the reality Unity runs in just one thread.