WaitForSeconds in C# outside of start or awake

Good Day

I tried to understand the WaitForSeconds method trough the documentation and afterwards by looking here trough many questions but I still can’t get it to work.

My setup is as follows:

  void FlipCardFaceUp(Card card_)
	{
		card_.isFaceUp = true;
		aCardsFlipped.Add(card_);
		if(aCardsFlipped.Count == 2)
		{
		playerCanClick = false;
		StartCoroutine(WaitASec(2));
		
		((Card)aCardsFlipped[0]).isFaceUp = false;
		((Card)aCardsFlipped[1]).isFaceUp = false;
		
		aCardsFlipped = new ArrayList();
		
		playerCanClick = true;
	}
}
IEnumerator WaitASec(float waitTime) {
    yield return new WaitForSeconds(waitTime);
   }

void OnGUI (){
		if(GUILayout.Button((Texture)Resources.Load(card.GetImage()), GUILayout.Width(cardW)))
			{
			FlipCardFaceUp(card);
			}
}

But It doesn’t work, the game doesn’t wait as if the function is not called. How exactly would I need to set it up so it will work?

If anyone can help me further with this I would appreciate it.

When you call use StartCoroutine without yielding, then it won’t wait for the coroutine to finish, it will just launch the coroutine and continue on with the other instructions. There’s no point to your WaitASec function, since it just calls WaitForSeconds and nothing else. You should remove that function, and just use yield WaitForSeconds directly in your FlipCardFaceUp function, which should be a coroutine.