Using yield WaitForSeconds() in C#

I’m currently reading Packtpub.Unity.3D.Game.Development.by.Example.Beginners’s Guide and trying to make Robot Repair game following instructions in the book. But instead of using Javascript like the book said, my stubborn brain (or stupid brain) told me to make it with C#, and here is the problem:

I meet this Javascript function and don’t know what to do to make yeild WaitForSecons works in C#

function FlipCardFaceUp(card:Card)
{
  card.isFaceUp = true;
  aCardsFlipped.Add(card);
  if(aCardsFlipped.Count == 2)
  {
    playerCanClick = false;
         
    yield WaitForSeconds(1);
         
    aCardsFlipped[0].isFaceUp = false;
    aCardsFlipped[1].isFaceUp = false;
    aCardsFlipped = new ArrayList();
      
    playerCanClick = true;
   }
}

I tried

void FlipCardFaceUp(Card aCard){
    				 
    		aCard.isFacedUp = true;
    		
    		if (aCardFlipped.IndexOf(aCard)<0)
    		{    			
    			aCardFlipped.Add(aCard);
    			if (aCardFlipped.Count == 2)
    			{
    				playerCanClick = false;
    				
    				StartCoroutine(WaitAndPrint(2.0F));
    			
    				aCardFlipped[0].isFacedUp = false;
    				aCardFlipped[1].isFacedUp = false;
    				aCardFlipped = new List<Card>();

    				playerCanClick = true;
    				    				
    			}			
    						
    		}
    		
    		
    	}	

with WaitAndPrint(2.0F) is from Unity example, but it doesn’t work.
Anyone knows where did I go wrong? Thank you in advance.

  • You need to specify the return value of FlipCardFaceUp as IEnumerator
  • You have to use “yield return new WaiForSeconds(0.2f);” to yield the function
  • You must call FlipCardFaceUp with the function StartCoroutine, or it won’t yield anything.

Oh, and make sure you haven’t removed “using System.Collections;”