Time for action – comparing the IDs

To compare the IDs, we need to make some changes to the FlipCardFaceUp function.

  1. Note that we're folding two existing lines of code inside a new conditional statement:
       function FlipCardFaceUp(card:Card)
       {
         card.isFaceUp = true;
          
         if(aCardsFlipped.IndexOf(card) < 0)
         {
           aCardsFlipped.Add(card);
       
           if(aCardsFlipped.Count == 2)
           {
             playerCanClick = false;
                
             yield WaitForSeconds(1);
                
             if(aCardsFlipped[0].id == aCardsFlipped[1].id)
             {
                  // Match!
                  aCardsFlipped[0].isMatched = true;
                  aCardsFlipped[1].isMatched = true;
                               
             } else 
    {
                 aCardsFlipped[0].isFaceUp = false;
                 aCardsFlipped[1].isFaceUp = false;
             }   
                
             aCardsFlipped = new List.<Card>();
                
             playerCanClick = true;
           }
         }
       }

    Here, we check to see if the two flipped-over cards have the same ...

Get Unity 4.x Game Development by Example Beginner's Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.