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 - check for victory

Our matchesMade, matchesNeededToWin, and playerHasWon variables have been standing at the ready this whole time. Let's finally make use of them.

  1. Add these few lines to the FlipCardFaceUp function, where you're detecting a match:
    if(aCardsFlipped[0].id == aCardsFlipped[1].id)
    {
    // Match!
    aCardsFlipped[0].isMatched = true;
    aCardsFlipped[1].isMatched = true;
    matchesMade ++;
    if(matchesMade >= matchesNeededToWin)
    {
    playerHasWon = true;
    }
    
  2. Add a new function call to the OnGUI function:
    function OnGUI () {
    GUILayout.BeginArea (Rect (0,0,Screen.width,Screen.height));
    GUILayout.BeginHorizontal();
    BuildGrid();
    if(playerHasWon) BuildWinPrompt();
    GUILayout.EndHorizontal();
    GUILayout.EndArea();
    }
    
  3. And, now, we'll use some ...

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