O'Reilly logo

Unity 3D Game Development by Example by Ryan Henson Creighton

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Time for action - build the card-flipping function

The game will let the player flip over two cards. Then, it will pause for a brief moment and flip the cards back over. We'll worry about detecting matches for our grand finale in just a moment.

  1. Add the following code to your FlipCardFaceUp function:
    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;
    }
    }
    
  2. Then, make a small change to one line in the BuildGrid function:
    if(GUILayout.Button(Resources.Load(img), GUILayout.Width(cardW))) { if(playerCanClick) { FlipCardFaceUp(card); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required