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 - compare 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:Object)
    {
    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 ArrayList();
    playerCanClick = true;
    }
    }
    }
    

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

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