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 - modify the img argument

Because we're passing a new argument to the Card class, we have to modify Card to accept it.

  1. Change the Card class code from this:
    class Card extends System.Object
    {
    // (variables omitted for clarity)
    function Card()
    {
    img = "robot";
    }
    }
    

    to this:

    class Card extends System.Object
    {
    // (variables omitted for clarity)
    function Card(img:String)
    {
    this.img = img;
    }
    }
    
  2. Now, find the nested loop in the Start function where we added all our new cards to the aGrid array. Change it from this:
    for(var j:int=0; j<cols; j++)
    {
    aGrid[i][j] = new Card();
    }
    

    to this:

    for(var j:int=0; j<cols; j++)
    {
    var someNum:int = Random.Range(0,aCards.length);
    aGrid[i][j] = aCards[someNum];
    aCards.RemoveAt(someNum);
    }
    

What just happened? ...

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