Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Unity 3D Game Development by Example

Discussion in 'Community Learning & Teaching' started by UntoldEnt, Sep 24, 2010.

  1. rockysam888

    rockysam888

    Joined:
    Jul 28, 2009
    Posts:
    650
    Thanks, Num_T, but I still need to think about this again.
     
  2. Noxide

    Noxide

    Joined:
    May 31, 2011
    Posts:
    1
    Hi I'm new here so please forgive be if this question has already been asked before... And I know it has but I could never really figure out the answer.
    I can't seem to find out were the download links for the chapter 5 assets are. From what I got from the comments I was supposed to go to the packt website and that's were I found a link called 'on PacktLib' right under another link called 'Download PDF'. I clicked on the bottom link and it directed me to a new window were I found download links under an 'Attachments Code Downloads' Tab. The links however didn't work.
    I guess the question I'm asking is, Did I go to the right place? Do I need to purchase the online version of the book for the links to work? If anybody has the time could he or she please help me because the previous answers didn't give me enough information to understand were the download links are, (not that I'm not thankful for them however).
     
  3. rockysam888

    rockysam888

    Joined:
    Jul 28, 2009
    Posts:
    650
    http://www.packtpub.com/support?nid=6081
     
  4. Bacardie

    Bacardie

    Joined:
    Jun 16, 2011
    Posts:
    4
    Dear fellow Unity users,
    I'm just starting out on Unity 3D following the book of this thread. I'm up to Chapter 5 trying to build the "Robot Repair" game. Now, I keep getting an error about my var card:Object = aGrid[j];

    NullReferenceException: Object reference not set to an instance of an object
    GameScript.BuildGrid () (at Assets/Scripts/GameScript.js:71)
    GameScript.OnGUI () (at Assets/Scripts/GameScript.js:42)

    my code:
    Code (csharp):
    1. var cols:int = 4; //the number of columns in the card grid
    2. var rows:int = 4; //the number of rows in the card grid
    3. var totalCards:int = cols*rows; //the total number of cards
    4. var matchesNeededToWin:int = totalCards * 0.5; //if there are 16 cards, the player needs to find 8 matches to empty the grid
    5. var matchesMade:int = 0; //the number of matches already made, starts with 0
    6. var cardW:int = 100; //the width of each card = 100 pixels
    7. var cardH:int = 100; //the height of each card = 100 pixels
    8. var aCards:Array; //store all cards we create in this array
    9. var aGrid:Array; //this array will keep track of all shuffled, dealt cards
    10. var aCardsFlipped:ArrayList; //this array will store the two cards that the player flips over
    11. var playerCanClick:boolean; //this boolean defines wether the player is allowed to flip a card (= either true or false)
    12. var playerHasWon:boolean = false;
    13.  
    14. //var customSkin : GUISkin; //use custom skin, same custom skin as in TitleGUI script
    15.  
    16. function start ()
    17. {
    18.     playerCanClick = true;
    19.  
    20.     aCards = new Array();
    21.     aGrid = new Array();
    22.     aCardsFlipped = new ArrayList();   
    23.    
    24.     for (i=0; i<rows; i++)
    25.     {
    26.         aGrid[i] = new Array();
    27.        
    28.         for (j=0; j<cols; j++)
    29.         {
    30.             aGrid[i][j] = new Card();
    31.         }
    32.     }
    33. }
    34.  
    35.  
    36. function OnGUI ()
    37. {
    38.     //GUI.skin = customSkin;
    39.     GUILayout.BeginArea (Rect (0,0,Screen.width, Screen.height));
    40.     BuildGrid();
    41.     GUILayout.EndArea();
    42.     //print("Building grid!");
    43. }
    44.  
    45.  
    46. class Card extends System.Object
    47. {
    48.     var isFaceUp:boolean = false;
    49.     var isMatched:boolean = false;
    50.     var img:String;
    51.    
    52.     function Card()
    53.     {
    54.         img = "robot"; 
    55.     }
    56. }
    57.  
    58.  
    59. function BuildGrid()
    60. {
    61.     GUILayout.BeginVertical();
    62.     GUILayout.FlexibleSpace();
    63.     for(i=0; i<rows; i++)
    64.     {
    65.         GUILayout.BeginHorizontal();
    66.         GUILayout.FlexibleSpace();
    67.         for(j=0; j<cols; j++)
    68.         {
    69.             var card:Object = aGrid[i][j];
    70.             if (GUILayout.Button(Resources.Load(card.img), GUILayout.Width(cardW)))
    71.             {
    72.             //Debug.Log(card.img);
    73.             }
    74.         }
    75.         GUILayout.FlexibleSpace();
    76.         GUILayout.EndHorizontal();
    77.     }
    78.     GUILayout.FlexibleSpace();
    79.     GUILayout.EndVertical();
    80.     Debug.Log("img created");
    81. }
    82.  
    I can't figure out why it does that :s But when I change Resources.Load(card.img) to Resources.Load("robot") and remove var card:Object = aGrid[j]; it works fine, but i'll be in trouble later on when i NEED to use the Card function...

    I've been researching topics and references for hours now, but it seems I can't get it fixed...

    So, I would very much appreciate some help here.
    Thanks in advance!
     
  5. Bacardie

    Bacardie

    Joined:
    Jun 16, 2011
    Posts:
    4
    Anyone?

    (I hate these tiny things that ruin the effort and take the joy out of it.. :s )

    Please help!
     
  6. Catsby

    Catsby

    Joined:
    Nov 16, 2010
    Posts:
    37
    Tested your script and it looks like you made a tiny little mistake. Look up at your start function. Recall that the text says all functions you are declaring begin with an upper case. When you built your Start function you didn't uppercase the S and I guess the editor thought you were making your own different start function. When you run your script everything that should be performed when the script loads never occurs. This includes the 2D array of Card class instances. So when OnGUI starts calling the BuildGrid function the BuildGrid function can't assign the variable card with the object you want it to because that object hasn't been instantiated (or the array that it's supposed to be in for that matter).

    I think that explains what's happening... Somebody else explain it if I got it wrong.

    Hope that helps! :3
     
    Last edited: Jun 18, 2011
  7. Catsby

    Catsby

    Joined:
    Nov 16, 2010
    Posts:
    37
    To add to this discussion; I'm having a problem with the HeartBounce script from chapter 8 (I think). I followed the steps through the entire chapter and everything seems to working until I pay attention to the Bounce Count GUIText object. Occasionally it registers a hit as two hits or no hits. The Heart object itself bounces around just fine but I can't figure out why the collisions aren't being counted properly.

    Here's the script

    Code (csharp):
    1. var hitCount : GUIText;
    2. var numHits : int = 0;
    3. var hasLost : boolean = false;
    4. var bestScore : int = 0;
    5. var lastBest : int = 0;
    6. var velocityWasStored = false;
    7. var storedVelocity : Vector3;
    8.  
    9.  
    10. function OnCollisionEnter(col : Collision){
    11.    
    12.     if(col.gameObject.tag == "tray"){
    13.         Debug.Log("yes! hit tray!");
    14.         if(!velocityWasStored){
    15.             storedVelocity = rigidbody.velocity;
    16.             velocityWasStored = true;
    17.         }
    18.         if(rigidbody.velocity.y > 1){
    19.             numHits++;
    20.         }
    21.         rigidbody.velocity.y = storedVelocity.y;
    22.     }
    23. }
    24.  
    25.  
    26. function Update () {
    27.  
    28. var str : String = "";
    29.  
    30.     if(!hasLost){
    31.         str = numHits.ToString();
    32.     }
    33.     else{
    34.         str = "Hits: " + numHits.ToString() + "\nYour best: " + bestScore;
    35.         if(bestScore > lastBest) {
    36.             str += "\nNew Record!";
    37.         }  
    38.     }  
    39.     hitCount.text = str;
    40.     if(transform.position.y < -3){
    41.         if(!hasLost){
    42.             hasLost = true;
    43.             lastBest = bestScore;
    44.             if(numHits > bestScore){
    45.                 bestScore = numHits;
    46.             }
    47.         }
    48.     }
    49. }
    50.  
    51. function OnGUI(){
    52.     if(hasLost == true){
    53.         var buttonW : int = 100;
    54.         var buttonH : int = 50;
    55.        
    56.         var halfScreenW : float = Screen.width/2;
    57.         var halfButtonW : float = buttonW/2;
    58.        
    59.         if(GUI.Button(Rect(halfScreenW - halfButtonW, Screen.height * 0.8,
    60.             buttonW, buttonH), "Play Again")){
    61.                
    62.             numHits = 0;
    63.             hasLost = false;
    64.             transform.position  = Vector3(0.5, 2, -0.05);
    65.             rigidbody.velocity = Vector3(0, 0, 0);
    66.         }
    67.     }  
    68. }
    Thanks in advance!
     
  8. Bacardie

    Bacardie

    Joined:
    Jun 16, 2011
    Posts:
    4
    Thank you very much Catsby!
    From now on I'll remember that all the standard Unity functions should change color to orange once defined...
    By all the fiddling around, I at least now know this! ;) Thanks!
     
  9. ro4ddogg

    ro4ddogg

    Joined:
    Jun 20, 2011
    Posts:
    2
    Hello, I'm not sure if this has been answered in the thread yet but i guess it's worth a shot.

    I'm stuck in chapter 5, when i run the robot repair grid that we made using the arrays, the game screen is just white, not robot pictures. Does any one have any ideas? Thanks in advance.

     
  10. Bacardie

    Bacardie

    Joined:
    Jun 16, 2011
    Posts:
    4
    ro4ddogg, I ran it through Unitron and here seems to be your problem:

    Have another look at your function declarations, in particular OnGUI, this is supposed to be a standard function by Unity, therefore it's name should turn orange once declared. In your case it's written "OnGui" and it stays black. Therefore, it should be written like this: OnGUI, voila!

    Ps. one of your variables (isFaceUp) is also without upper-case 'u'. Just for consistency sake ;)
     
  11. ro4ddogg

    ro4ddogg

    Joined:
    Jun 20, 2011
    Posts:
    2
    Bacardie,

    Thank you very much, i was ripping may hair out. Can't believe i didn't notice that before. Thanks again.


     
  12. brandon199511

    brandon199511

    Joined:
    Sep 21, 2010
    Posts:
    268
    Where are the downloads?
     
  13. webgurl

    webgurl

    Joined:
    Jul 10, 2011
    Posts:
    1
    I experienced the same error(s). If you add the animations to the character model BEFORE you drag the character model to the Character Prefab it will work.

    If you follow the instructions from the book:
    Pg 184 - Time for action - add the character
    • Follow Step 1 (Create the prefab...)
    • Jump to the next page - Time for action - register the animations
    • Follow the instructions on this page (Step 1 - 7)
    • Now return to the previous page and continue on to Step 2.


     
  14. rockysam888

    rockysam888

    Joined:
    Jul 28, 2009
    Posts:
    650
    Hi all,

    Q1.
    On page 192 of chapter 7 about adding a clock to RobotRepair, how to change the color of text clock from black to other color?
    (I tried to change the main color of material to other color without success)

    the text still black.

    Q2.
    In function OnGUI, why we use new command?
    I try to remove "new", it still can run.
    What is the purpose of using "new Rect", instead of "Rect"?

    Thanks in advance
     
    Last edited: Jul 17, 2011
  15. ognjenm

    ognjenm

    Joined:
    May 3, 2011
    Posts:
    97
    i'm planing to buy this book, so my question is when i finish learning from this book how far i will advance?

    p.s i'm dont know anything about coding
    maybe only what are strings,vars,functions but dont know to use them
     
  16. Bepton

    Bepton

    Joined:
    Aug 1, 2011
    Posts:
    2
    Chapter 5, Robot Repair game. Please can somebody tell me the location of the Assets package so my son can download the Assets as he cannot complete the chapter/game. I have read some of the other postings and I purchased "Unity 3D Game Development by Example" from Amazon UK and no mention in the introduction about the need to register with Packtpub to get essential resources.
     
    Last edited: Aug 1, 2011
  17. Bepton

    Bepton

    Joined:
    Aug 1, 2011
    Posts:
    2
  18. Superflykalel

    Superflykalel

    Joined:
    Aug 2, 2011
    Posts:
    1
    Does anybody know why this will not work? I will greatly appreciate your help!




    var cols:int =4;
    var rows:int= 4;
    var totalCards:int = cols*rows;
    var matchesNeededToWin:int= totalCards*0.5;
    var matchesMade:int =0;
    var cardW:int=100;
    var cardH:int =100;
    var aCards: Array;
    var aGrid:Array;
    var aCardsFlipped:ArrayList;
    var playerCanClick:boolean;
    var playerHasWon:boolean= false;

    function Start(){
    playerCanClick= true;
    aCards= new Array();
    aGrid= new Array();
    aCardsFlipped= new ArrayList();
    BuildDeck();
    for (i=0; i< rows; i++)
    {
    aGrid = new Array();

    for (j=0; j<cols; j++){
    var somNum:int= Random.Range(0,aCards.length);
    aGrid[j]= aCards[someNum];
    aCards.RemoveAt(someNum);
    }
    }
    }
    class Card extends System.Object{
    var isFaceUp: boolean= false;
    var isMatched:boolean= false;
    var img:String;
    var id:int;

    function Card(img:String, id:int){
    this.img= img;
    this.id=id;
    }
    }


    function OnGUI () {
    GUILayout.BeginArea(Rect(0,0,Screen.width,Screen.height));
    GUILayout.BeginHorizontal();
    BuildGrid();
    if(playerHasWon) BuildWinPrompt();
    GUILayout.EndHorizontal();
    GUILayout.EndArea();
    }


    function BuildGrid(){
    GUILayout.BeginVertical();
    GUILayout.FlexibleSpace();

    for(var i: int=0; i<rows; i++){
    GUILayout.BeginHorizontal();
    GUILayout.FlexibleSpace();
    for(var j:int=0; j<cols; j++){

    var Card: Object= aGrid[j];
    var img:String;

    if(card.isMatched){
    img = "blank";
    }
    else{
    if(card.isFaceUp){
    img= card.img;
    }
    else{
    img= "wrench";
    }
    }

    GUI.enabled= !card.isMatched;

    if(GUILayout.Button(Resources.Load(img), GUILayout.Width(cardW))){
    if(playerCanClick) FlipCardFaceUp(card);
    {


    GUI.enabled=true;
    }
    GUILayout.FlexibleSpace();
    GUILayout.EndHorizontal();
    }
    GUILayout.FlexibleSpace();
    GUILayout.EndVertical();
    }

    function BuildDeck(){
    var totalRobots:int = 4;
    var card: Object;
    var id:int=0;
    for(i=0; i<totalRobots; i++){

    var aRobotParts:Array = ["Head", "Arm", "Leg"];
    for(j=0; j<2; j++){

    var someNum:int= Random.Range(0, aRobotParts.length);
    var theMissingPart: String = aRobotParts[someNum];
    aRobotParts.RemoveAt(someNum);
    card = new Card("robot" + (i+1) + "Missing" + theMissingPart,id);
    aCards.Add(card);
    card=new Card("robot" + (i+1) + theMissingPart, id);
    aCards.Add(card);
    id++;
    }
    }
    }
    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;
    matchesMade ++;
    if(matchesMade>= matchesNeededToWin){
    playerHasWon= true;
    }
    }
    else{
    aCardsFlipped[0].isFaceUp= false;
    aCardsFlipped[1].isFaceUp= false;
    }
    aCardsFlipped= new ArrayList();
    playerCanClick= true;
    }
    }
    function BuildWinPrompt(){
    var winPromptW: int=100;
    var winPromptH= 90;

    var halfScreenW:float= Screen.width*0.5;
    var halfScreenH:float= Screen.height*0.5;

    var halfPromptW: int = winPromptW* 0.5;
    var halfPromptH: int = winPromptH* 0.5;

    GUI. BeginGroup(Rect(halfScreenW-halfPromptW, halfScreenH- halfPromptH,winPromptW, winPromptH));
    GUI.Box(Rect(0,0,winPromptW, winPromptH), "you win");
    if(GUI.Button(Rect(10,40,80,20), "play again")){
    Application.LoadLevel("title");
    }
    GUI.EndGroup();




    }
     
  19. lukasaurus

    lukasaurus

    Joined:
    Aug 21, 2011
    Posts:
    360
    Will these books still be beneficial now, even though they are for older versions of Unity? Thinking of picking up the eBooks, but wondering if I should wait until newer versions are released
     
  20. rockysam888

    rockysam888

    Joined:
    Jul 28, 2009
    Posts:
    650
  21. lukasaurus

    lukasaurus

    Joined:
    Aug 21, 2011
    Posts:
    360
    Yeah, thing is, I need to teach Javascript to a group of high school students next year, so I need a headstart on it. I am a Technology teacher, but have focused more on website development, advertising and graphics than on programming. However, the school I am at wants my dept to begin teaching programming, possibly having two streams of Technology in the future. I'm keen for this to happen as well, since it motivates me to learn. I'm going through the walker boys tutorials and I am thinking that the level of complexity they get up to will be where we head towards. I've decided that if I am the one teaching it, we'll be using Unity. These are 7th form (final year) high school students, and while we have used Scratch, Alice and Game Maker at lower levels, I think using C# or Javascript will really be more beneficial for any of them who choose to go on and do some kind of comp sci at uni.

    In short, any idea when that book gets released? It looks good and I've read some great reviews!
     
  22. rockysam888

    rockysam888

    Joined:
    Jul 28, 2009
    Posts:
    650
  23. OldRod

    OldRod

    Joined:
    Mar 2, 2009
    Posts:
    136
    So, I just broke down and bought this book a couple days ago and went looking on the publisher's site for any corrections/errata that might be out... and I see there's a version of this book for Unity 3.0 listed. Is this out? Or is there a release date?
     
  24. WedgeBob

    WedgeBob

    Joined:
    Jul 17, 2011
    Posts:
    122
    Might have to look at this from Amazon. I know I bought another book from Amazon called Game Development with Unity, which was okay, even tho that covers JavaScript instead of C#, which didn't turn me on, but I will look at this one for sure.
     
  25. caffeine

    caffeine

    Joined:
    Aug 31, 2011
    Posts:
    7
    I eliminated all errors from my Robot Repair script, but it has an unwanted feature: When a match is made the cards go gray instead of blank. They are still pickable, and they go blank and unpickable if you make a second match with them. It's like a game inside a game. Could someone take a look? Without errors I can't track it down.


    Code (csharp):
    1. var cols:int = 4;
    2. var rows:int = 4;
    3. var totalCards:int = cols*rows;
    4. var matchesNeededToWin:int = totalCards * 0.5;
    5. var matchesMade:int = 0;
    6. var cardW:int = 100; //pixel width of card
    7. var cardH:int = 100; //pixel height of card
    8. var aCards:Array; //stores created cards
    9. var aGrid:Array; //keeping track of shuffled and dealt cards
    10. var aCardsFlipped:ArrayList; //this array is for Storing the two cards flipped
    11. var playerCanClick:boolean;
    12. var playerHasWon:boolean = false;
    13.  
    14. function Start()
    15. {
    16.     playerCanClick = true;
    17.     aCards = new Array();
    18.     aGrid = new Array();
    19.     aCardsFlipped = new ArrayList();
    20.     BuildDeck();
    21.    
    22.     for(var i=0; i<rows; i++)
    23.     {
    24.         aGrid[i] = new Array(); //new Array at index? i
    25.        
    26.         for(var j=0; j<cols; j++)
    27.         {
    28.             var someNum:int = Random.Range(0,aCards.length);
    29.             aGrid[i][j] = aCards[someNum];
    30.             aCards.RemoveAt(someNum);
    31.         }
    32.     }
    33. }
    34.  
    35. class Card extends System.Object
    36. {
    37.     var isFaceUp:boolean = false;
    38.     var isMatched:boolean = false;
    39.     var img:String; //stores the name of the Card
    40.     var id:int;
    41.    
    42.     function Card(img:String, id:int)
    43.     {
    44.         this.img = img;
    45.         this.id = id;
    46.     }
    47. }
    48.  
    49. function OnGUI () {
    50.     GUILayout.BeginArea (Rect (0,0,Screen.width, Screen.height));
    51.     GUILayout.BeginHorizontal();
    52.     BuildGrid();
    53.     if(playerHasWon) BuildWinPrompt();
    54.     GUILayout.EndHorizontal();
    55.     GUILayout.EndArea();
    56. }
    57.  
    58. function BuildGrid()
    59. {
    60.     GUILayout.BeginVertical();
    61.     GUILayout.FlexibleSpace();
    62.     for(i=0; i<rows; i++)
    63.     {
    64.         GUILayout.BeginHorizontal();
    65.         GUILayout.FlexibleSpace();
    66.         for(j=0; j<cols; j++)
    67.         {
    68.             var card:Object = aGrid[i][j];
    69.             var img:String;
    70.            
    71.             if(card.isMatched)
    72.             {
    73.                 img = "blank";
    74.             }
    75.             else
    76.             {
    77.                 if(card.isFaceUp)
    78.                 {
    79.                     img = card.img;
    80.                 }
    81.                 else
    82.                 {
    83.                     img = "wrench";
    84.                 }
    85.             }
    86.            
    87.             GUI.enabled = !card.isMatched;
    88.             if(GUILayout.Button(Resources.Load(img), GUILayout.Width(cardW)))
    89.             {
    90.                 if(playerCanClick) //heres where the book has an error
    91.                 {
    92.                     FlipCardFaceUp(card);
    93.                 }
    94.             Debug.Log(card.img);
    95.             }
    96.             GUI.enabled = true;
    97.         }
    98.         GUILayout.FlexibleSpace();
    99.         GUILayout.EndHorizontal();
    100.     }
    101.     GUILayout.FlexibleSpace();
    102.     GUILayout.EndVertical();
    103. }
    104.  
    105. function BuildDeck()
    106. {
    107.     var totalRobots:int = 4; // defines how many robots there are
    108.     var card:Object; //stores reference to a card
    109.     var id:int = 0;
    110.     for(i=0; i<totalRobots; i++)
    111.     {
    112.         var aRobotParts:Array = ["Head", "Arm", "Leg"];
    113.         for(j=0; j<2; j++)
    114.         {
    115.             var someNum:int = Random.Range(0, aRobotParts.length);
    116.             var theMissingPart:String = aRobotParts[someNum];
    117.             aRobotParts.RemoveAt(someNum);
    118.             card = new Card("robot" + (i+1) + "Missing" + theMissingPart, id);
    119.             aCards.Add(card);
    120.             card = new Card("robot" + (i+1) + theMissingPart, id);
    121.             aCards.Add(card);
    122.             id++;
    123.         }
    124.     }
    125. }
    126.  
    127. function FlipCardFaceUp(card:Card)
    128. {
    129.     card.isFaceUp = true;
    130.     if(aCardsFlipped.IndexOf(card) < 0)
    131.     {
    132.         aCardsFlipped.Add(card);
    133.     }
    134.     if(aCardsFlipped.Count == 2)
    135.     {
    136.         playerCanClick = false;
    137.        
    138.         yield WaitForSeconds(1);
    139.        
    140.         if(aCardsFlipped[0].id == aCardsFlipped[1].id)
    141.         {
    142.             //match!!
    143.             aCardsFlipped[0].isMatched = true; //check
    144.             aCardsFlipped[1].isMatched = true;
    145.            
    146.             matchesMade ++;
    147.            
    148.             if(matchesMade >= matchesNeededToWin)
    149.             {
    150.                 playerHasWon = true;
    151.             }
    152.         }
    153.         else
    154.         {
    155.             aCardsFlipped[0].isFaceUp = false; //check
    156.             aCardsFlipped[1].isFaceUp = false;
    157.         }
    158.         aCardsFlipped = new ArrayList(); //open the array again
    159.        
    160.         playerCanClick = true;
    161.     }
    162. }
    163.  
    164. function BuildWinPrompt()
    165. {
    166.     var winPromptW:int = 100;
    167.     var winPromptH:int = 90;
    168.    
    169.     var halfScreenW:float = Screen.width * 0.5;
    170.     var halfScreenH:float = Screen.height * 0.5;
    171.    
    172.     var halfPromptW:int = winPromptW * 0.5;
    173.     var halfPromptH:int = winPromptH * 0.5;
    174.    
    175.     GUI.BeginGroup(Rect(halfScreenW-halfPromptW, halfScreenH-halfPromptH, winPromptW, winPromptH));
    176.     GUI.Box (Rect (0,0,winPromptW,winPromptH), "you win");
    177.     if(GUI.Button(Rect(10,40,80,20), "play again"))
    178.     {
    179.         Application.LoadLevel("title");
    180.     }
    181.     GUI.EndGroup();
    182. }
     
    Last edited: Oct 13, 2011
  26. Slitharg

    Slitharg

    Joined:
    Jun 17, 2011
    Posts:
    1
    I bought the book but I don't know how to get the assets for the robot repair game can someone help?
     
  27. kingcharizard

    kingcharizard

    Joined:
    Jun 30, 2011
    Posts:
    1,137
    This is a very impressive book so far, and I must say its the first book that has not bored me to death... I love how you started the book telling would be game developers not to start big and you've pointed out why too.. Great Job.. so far.. I've just made it to chapter 2. i'll post again when I finish the book to let you know if i learned anything.
     
  28. Shadows

    Shadows

    Joined:
    Dec 5, 2011
    Posts:
    1
    Hey all.

    I've ran into a little bottle neck and would really appreciate some help.

    I'm currently on chapter 7; writing the clockScript for robot repair

    At the moment it comes up with this error:

    Here is my script:

    Code (csharp):
    1. var isPaused : boolean = false; //holds the Pause state
    2. var startTime : float; //(In seconds)
    3. var timeRemaining : float; //(In seconds)
    4.  
    5.  
    6.  
    7. function Start() {
    8.     startTime = 5.0;
    9.  
    10.  
    11. }
    12.  
    13.  
    14.  
    15.  
    16. function Update() {
    17.     if (!isPaused);
    18. {
    19.     //Make sure the timer is not paused
    20.      DoCountdown();
    21.      
    22.      
    23.     }
    24.    
    25. }
    26.  
    27.  
    28.  //Function that is called every update, as long as the isPaused flag is false
    29. function DoCountdown() {
    30.  
    31. Debug.Log("time remaining = " + timeRemaining);
    32.    
    33.     timeRemaining = StartTime - Time.time;
    34.         if(timeRemaining < 0)
    35.         {
    36.         timeRemaining = 0;
    37.         isPaused = true;
    38.         TimeIsUp();
    39.         }
    40. }
    41.  
    42. function PauseClock() {
    43.     isPaused = true;
    44. }
    45.  
    46. function UnpauseClock() {
    47.     isPaused = false;
    48. }
    49.  
    50. //Displays the time in the GUIText component
    51. function ShowTime() {
    52. }
    53.  
    54. function TimeIsUp() { //Executes when the time reaches zero
    55.     Debug.Log("Timeis up!");
    56. }
    Thanks in advance.:)

    EDIT: error solved:

    Correct script here if anyone experiences the same issue:

    Code (csharp):
    1. var isPaused : boolean = false; //holds the Pause state
    2. var startTime : float; //(In seconds)
    3. var timeRemaining : float; //(In seconds)
    4.  
    5.  
    6.  
    7. function Start() {
    8.     startTime = 5.0;
    9.  
    10.  
    11. }
    12.  
    13.  
    14.  
    15.  
    16. function Update() {
    17.     if (!isPaused) //Book error is here ignore putting a ';' at the end
    18. {
    19.     //Make sure the timer is not paused
    20.      DoCountdown();
    21.      
    22.      
    23.     }
    24.    
    25. }
    26.  
    27.  
    28.  //Function that is called every update, as long as the isPaused flag is false
    29. function DoCountdown() {
    30.  
    31. Debug.Log("time remaining = " + timeRemaining);
    32.    
    33.     timeRemaining = startTime - Time.time;
    34.         if(timeRemaining < 0)
    35.         {
    36.         timeRemaining = 0;
    37.         isPaused = true;
    38.         TimeIsUp();
    39.         }
    40. }
    41.  
    42. function PauseClock() {
    43.     isPaused = true;
    44. }
    45.  
    46. function UnpauseClock() {
    47.     isPaused = false;
    48. }
    49.  
    50. //Displays the time in the GUIText component
    51. function ShowTime() {
    52. }
    53.  
    54. function TimeIsUp() { //Executes when the time reaches zero
    55.     Debug.Log("Time is up!");
    56. }
     
    Last edited: Dec 8, 2011
  29. 24seven

    24seven

    Joined:
    Mar 31, 2012
    Posts:
    2
    Hi Ryan,
    THANKS for a great book! I just finished Chapter 6 - Robot Repair Part 2 and the program works just fine. When I run the program in MonoDevelop (to follow the execution of the code and better understand how the program flows,) the code of BuildGrid() keeps repeating or seems to be in a loop. I tried to click on one of the images ("wrench") in the grid but nothing happens. Below is the code for your reference:

    var cols:int = 4;
    var rows:int = 4;
    var totalCards:int = 16;
    var matchesNeededToWin:int = totalCards * 0.5;
    var matchesMade:int = 0;
    var cardW:int = 100;
    var cardH:int = 100;
    var aCards:Array;
    var aGrid:Array;
    var aCardsFlipped:ArrayList;
    var playerCanClick:boolean;
    var playerHasWon:boolean = false;

    function Start()
    {
    playerCanClick = true;
    aCards = new Array();
    aGrid = new Array();
    aCardsFlipped = new ArrayList();

    BuildDeck();

    for(i=0; i<rows; i++)
    {
    aGrid = new Array();

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

    var customSkin:GUISkin;
    function OnGUI ()
    {
    GUILayout.BeginArea (Rect (0,0,Screen.width,Screen.height));
    GUILayout.BeginHorizontal();
    BuildGrid();
    If (playerHasWon) BuildWinPrompt();
    GUILayout.EndHorizontal();
    GUILayout.EndArea();
    print("***BUILDING GRID***");
    }

    class Card extends System.Object
    {
    var isFaceUp:boolean = false;
    var isMatched:boolean = false;
    var img:String;
    var id:int;

    function Card(img:String, id:int)
    {
    this.img = img;
    this.id = id;
    }
    }

    function BuildGrid()
    {
    GUILayout.BeginVertical();
    GUILayout.FlexibleSpace();

    for(i=0; i<rows; i++)
    {
    GUILayout.BeginHorizontal();
    GUILayout.FlexibleSpace();

    for(j=0; j<cols; j++)
    {
    var card:Object = aGrid[j];
    var img:String;

    if(card.isMatched)
    {
    img = "blank";
    }
    else
    {
    if(card.isFaceUp)
    {
    img = card.img;
    }
    else
    {
    img = "wrench";
    }
    }

    GUI.enabled = !card.isMatched;
    if(GUILayout.Button(Resources.Load(img), GUILayout.Width(cardW)))
    {
    if(playerCanClick)
    {
    FlipCardFaceUp(card);
    }
    Debug.Log(card.img);
    }
    GUI.enabled = true;
    }
    GUILayout.FlexibleSpace();
    GUILayout.EndHorizontal();
    }
    GUILayout.FlexibleSpace();
    GUILayout.EndVertical();
    }

    function BuildDeck()
    {
    var totalRobots:int = 4;
    var card:Object;
    var id:int = 0;

    for(i=0; i<totalRobots; i++)
    {
    var aRobotParts:Array = ["Head", "Arm", "Leg"];

    for(j=0; j<2; j++)
    {
    var someNum:int = Random.Range(0, aRobotParts.length);
    var theMissingPart:String = aRobotParts[someNum];

    aRobotParts.RemoveAt(someNum);

    card = new Card("robot" + (i+1) + "Missing" + theMissingPart, id);
    aCards.Add(card);

    card = new Card("robot" + (i+1) + theMissingPart, id);
    aCards.Add(card);
    id++;
    }
    }
    }

    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)
    {
    aCardsFlipped[0].isMatched = true;
    aCardsFlipped[1].isMatched = true;

    matchesMade ++;

    if (matchesMade >= matchesNeededToWin)
    {
    playerHasWon = true;
    }
    }
    else
    {
    aCardsFlipped[0].isFaceUp = false;
    aCardsFlipped[1].isFaceUp = false;
    }

    aCardsFlipped = new ArrayList();
    playerCanClick = true;
    }
    }
    }

    function BuildWinPrompt()
    {
    var winPromptW:int = 100;
    var winPromptH:int = 90;

    var halfScreenW:float = Screen.width/2;
    var halfScreenH:float = Screen.height/2;

    var halfPromptW:int = winPromptW/2;
    var halfPromptH:int = winPromptH/2;

    GUI.BeginGroup(Rect(halfScreenW - halfPromptW, halfScreenH - halfPromptH, winPromptW, winPromptH));
    GUI.Box (Rect(0,0,winPromptW, winPromptH), "A Winner is YOU!!!");
    if (GUI.Button(Rect(10,40,80,20), "Play Again"))
    {
    Application.LoadedLevel("Title");
    }
    GUI.EndGroup();
    }
     
  30. lukasaurus

    lukasaurus

    Joined:
    Aug 21, 2011
    Posts:
    360
    Great book. I'm finding it really useful, although it will definitely be over the heads of most of my students (I think a few could probably grasp it).

    A tip to anyone following the book. Go through and add your own code comments. It is a really good way of making sure you understand everything that is happening.

    for example, on the Robot Repair tutorial, I went through and commented each line, like this

    Code (csharp):
    1.  
    2. function BuildDeck() //creates the deck of robots
    3. {
    4.     var totalRobots:int = 4; //how many different robots to create
    5.     var card:Object; //create a new variable to hold an object
    6.     for (var i = 0;i<totalRobots;i++) //iterative loop to create # of robots declared above
    7.     {
    8.         var aRobotParts:Array = ["Head","Arm","Leg"]; //create a new variable containing all the robot parts
    9.         for (var j=0;j<2;j++) //iterative loop to create each robot pair
    10.         {
    11.             var someNum: int = Random.Range(0,aRobotParts.length); //determine a random number between 0 and 2
    12.             var theMissingPart:String = aRobotParts[someNum]; //declare a variable of type string with the value of the random number in Array
    13.            
    14.             aRobotParts.RemoveAt(someNum); //remove that Robot piece from the array
    15.            
    16.             card = new Card("robot" + (i+1) + "Missing" + theMissingPart); //create a new card for the missing part
    17.             aCards.Add(card); //add that card to the deck
    18.            
    19.             card = new Card("robot" + (i+1) + theMissingPart); //create a new card for the robot with the missing part
    20.             aCards.Add(card); //add that card to the deck
    21.             //the deck will change each game since each robot can have three pairs, but only two are selected in any one game
    22.         }
    23.     }
    24. }
    25.  
    A lot of the comments are useless, but I put them in anyway, because it forces me to read each line and actually understand what it is doing.

    I also made some modifications, which is another good way to make sure you understand.

    eg, I took some stuff out of start and made another function (ShuffleDeck), to make sure I knew what was happening. When I was just typing in the code, I'll be honest, I didn't see where a lot of it was going, but by taking the time to go back and add your own comments, you learn :)
     
    Last edited: Apr 8, 2012
  31. lukasaurus

    lukasaurus

    Joined:
    Aug 21, 2011
    Posts:
    360
    Did you read the instructions in the front of the book? They are available at the packtpub.com website under "Support".
     
  32. 24seven

    24seven

    Joined:
    Mar 31, 2012
    Posts:
    2
    Thanks for the tip... comments are definitely very helpful! But running the game in MonoDevelop was most helpful to me to follow the program flow. I was able to place breakpoints, step into and out of each code, and see the values of the variables and objects. But I ran into a wall when the code of BuildGrid() keeps repeating or seems to be in a loop. I tried to click on one of the images ("wrench") in the grid but nothing happens:

    function OnGUI ()
    {
    GUILayout.BeginArea (Rect (0,0,Screen.width,Screen.height));
    GUILayout.BeginHorizontal();
    BuildGrid();
    if (playerHasWon) BuildWinPrompt(); // it's looping starting from here and the next 3 statements
    GUILayout.EndHorizontal();
    GUILayout.EndArea();
    print("***BUILDING GRID***");
    }

    Would anyone have any idea why this is happenning? I'm new to MonoDevelop so I may not be using it correctly. Thanks.
     
  33. Bulletghost4

    Bulletghost4

    Joined:
    Apr 29, 2012
    Posts:
    3
    Hi

    bought the book yesterday, i am currently on the Robot Repair chapter and i have just "completed" the part where i am supposed to get the 4x4 grid of robots on my screen when i play (page 283)...sadly it's not. Could someone take a look at my script and tell me what i am doing wrong. Great book by the way, brilliant for beginners (like me :D), i am enjoying learning programming so much and i would really hate to be stuck on this chapter for eternity..

    Thanks!

    -BG

    ha, i forgot to put in my GameScript code.. here it is!

    var cols:int = 4; //number of columns in card grid
    var rows:int = 4; //number of rows in card grid
    var totalCards:int = cols*rows; //...yeah..that
    var matchesNeededToWin:int = totalCards*0.5; // if there are 16 cards, you need 8 matches to win
    var matchesMade:int = 0; //at the outset, the player hasn't made any matches
    var cardW:int = 100;
    var cardH:int = 100;
    var aCards:Array; //we'll store all the cards we create in this array
    var aGrid:Array; //this will keep track of the shuffled, dealt cards
    var aCardsFlipped:ArrayList; //will store 2 cards the player flips over
    var playerCanClick:boolean; //use this flag to prevent player from clicking buttons we dont want him to
    var playerHasWon:boolean = false; //store whether or not the player has won, probably should start false ;D

    function Start()
    {
    playerCanClick = true;
    //initialises the arrays as empty lists:
    aCards = new Array();
    aGrid = new Array();
    aCardsFlipped = new ArrayList();
    for(i=0; i<rows; i++)
    {
    aGrid = new Array(); //create a new empty array at index i
    for (j=0; j<cols; j++)
    {
    aGrid[j] = new Card();
    }
    }
    }

    function Update () {

    }

    class Card extends System.Object
    {
    var isFaceUp:boolean = false;
    var isMatched:boolean = false;
    var img:String;
    function Card()
    {
    img = "robot";
    }
    }

    function OnGUI ()
    {
    GUILayout.BeginArea (Rect(0,0,Screen.width,Screen.height));
    BuildGrid();
    GUILayout.EndArea();
    print("Building Grid");
    }

    function BuildGrid()
    {
    GUILayout.BeginVertical();
    for(i=0; i<rows; i++)
    {
    GUILayout.BeginHorizontal();
    for(j=0;j<cols;j++)
    {
    var card:Object = aGrid[j];
    if(GUILayout.Button(Resources.Load(card.img),GUILayout.Width(cardW)))
    {
    Debug.Log(card.img);
    }
    }
    GUILayout.EndHorizontal();
    }
    GUILayout.EndVertical();
    }
     
    Last edited: Jun 27, 2012
  34. Renebart

    Renebart

    Joined:
    Apr 4, 2012
    Posts:
    2
    Well, I finish the Robot Repair tutorial and clockTimer.

    How I call from clockScript and say the gameScript time is over. (Done)

    example:

    When the timer finish.
    1. Call a GUI and say Game Over, try again. or Exit.
    2. if player play again ,Send the player to start menu. (DONE)
    3. Set clock again. if set to 60 seconds. Start time again in 60 seconds when start new game.
    4. Add Option in menu Easy, Medium, Hard. (DONE)
     
    Last edited: Jul 2, 2012
  35. sincor

    sincor

    Joined:
    Jul 3, 2012
    Posts:
    1
    i thanks
     
  36. BeastlyProdigy

    BeastlyProdigy

    Joined:
    Aug 17, 2012
    Posts:
    1
    Can anyone tell me what's wrong with this? Im on chapter 5 page 147, its telling me there are compiler errors.

    #pragma strict
    var cols:int = 4;
    var rows:int = 4;
    var totalCard:int = cols*rows;
    var matchesNeededToWin:int =totalCards * 0.5;
    var matchesMade:int = 0;
    var cardW:int = 100;
    var cardH:int = 100;
    var aCards:Array;
    var aGrid:Array;
    var aCardsFlipped:ArrayList;
    var playerCanClick:boolean;
    var playerHasWon:boolean = false;
    function Start () {
    playerCanclick = true;
    aCards = new Array();
    aGrid = new Array();
    aCardsFlipped = new ArrayList();
    for(i=0; i<rows; i++)
    {
    aGrid = new Array();
    for(j=0; j<cols; j++)
    {
    aGrid[j] = new Card();
    }
    }
    }
    class Card extends System.Object
    {
    var isFaceUp:boolean = false;
    var isMatched:boolean = false;
    var img:String;
    funtion Card()
    {
    img = "robot";
    }
    }
    function OnGUI ()
    {
    GUILayout.BeginArea (Rect(0,0,Screen.width,Screen.height));
    BuildGrid()
    GUILayout.EndArea();
    print("building grid!");
    }
    function BuildGrid()
    {
    GUILayout.BeginVertical();
    for(i=0; i<rows; i++)
    {
    GUILayout.BeginHorizontal();
    for(j=0; j<cols; j++)
    {
    var card:Object = aGrid[j];
    if(GUILayout.Button(Resources.Load(card.img),
    GUILayout.Width(cardW)))
    {
    Debug.Log(card.img);
    }
    }
    GUILayout.EndHorizontal();
    }
    GUILayout.EndVertical();
    }
     
  37. Aligrid

    Aligrid

    Joined:
    Aug 30, 2012
    Posts:
    1
    where can i get robot repair asset ??
     
  38. rockysam888

    rockysam888

    Joined:
    Jul 28, 2009
    Posts:
    650
    Last edited: Aug 30, 2012
  39. skine

    skine

    Joined:
    Jul 3, 2012
    Posts:
    12
  40. Zoltar26

    Zoltar26

    Joined:
    Sep 6, 2012
    Posts:
    17
    Hi folks,
    Unfortunately I also need your good help in solving a code error:
    Chapter 6, page number 168:
    "Save the script and play your game! What you should see is an army of amputated androids.
    Awesome! Because the... etc."


    Error 1:
    ArgumentOutOfRangeException: Index is less than 0 or more than or equal to the list count.
    Parameter name: index
    0
    System.Collections.ArrayList.ThrowNewArgumentOutOfRangeException (System.String name, System.Object actual, System.String message)
    System.Collections.ArrayList.get_Item (Int32 index)
    UnityScript.Lang.Array.get_Item (Int32 index)
    GameScript.Start () (at Assets/Scripts/GameScript.js:36)

    Error 2:
    ArgumentOutOfRangeException: Index is less than 0 or more than or equal to the list count.
    Parameter name: index
    0
    System.Collections.ArrayList.ThrowNewArgumentOutOfRangeException (string,object,string) <IL 0x00008, 0x00064>
    System.Collections.ArrayList.get_Item (int) <IL 0x00023, 0x00083>
    UnityScript.Lang.Array.get_Item (int) <IL 0x00007, 0x0003b>
    (wrapper dynamic-method) UnityScript.Lang.Array.Array$get_Item$System.Int32 (object,object[]) <IL 0x00017, 0x00082>
    Boo.Lang.Runtime.RuntimeServices.GetSlice (object,string,object[]) <IL 0x00056, 0x00137>
    GameScript.BuildGrid () (at Assets/Scripts/GameScript.js:63)
    GameScript.OnGUI () (at Assets/Scripts/GameScript.js:45)

    Code:

    Code (csharp):
    1.  
    2. var cols:int = 4; // the number of columns in the card grid
    3. var rows:int = 4; // the number of rows in the card grid
    4. var totalCards:int = cols*rows;
    5. var matchesNeededToWin:int = totalCards*0.5; // if there are 16 cards, the player needs
    6.                                              // to find 8 matches to clear the board
    7. var matchesMade:int = 0; // at the outset, the player has not made any matches
    8. var cardW:int = 100; // each card's width and heigth is 100 pixels                                           
    9. var cardH:int = 100;
    10. var aCards:Array; // we'll store all the cards we create in this array
    11. var aGrid:Array; // this array will keep track of the shuffled, dealt cards
    12. var aCardsFlipped:ArrayList; // this array will store the two cards
    13.                              // that the player flips over
    14. var playerCanClick:boolean; // we'll use this flag to prevent the player from
    15.                             // clicking buttons when we don't want him to      
    16. var playerHasWon:boolean = false; // store whether or not the player has won                                                 
    17.  
    18. // the very first function that gets called.
    19. function Start()
    20. {
    21.     playerCanClick = true; // enables the player to play
    22.    
    23.     // Initialize the array as empty lists
    24.     aCards = new Array();
    25.     aGrid = new Array();
    26.     aCardsFlipped = new ArrayList();
    27.    
    28.     BuildDeck();
    29.    
    30.     for(i=0; i<rows; i++)
    31.     {
    32.         aGrid[i] = new Array(); // create a new, empty array at index i
    33.        
    34.         for(j=0; j<cols; j++)
    35.         {
    36.             var someNum:int = Random.Range(0,aCards.length);
    37.             aGrid[i][j] = aCards[someNum];
    38.             aCards.RemoveAt(someNum);
    39.         } // end for
    40.     } // end for
    41. } // end function Start()
    42.  
    43. function OnGUI ()
    44. {
    45.     GUILayout.BeginArea(Rect(0,0,Screen.width,Screen.height));
    46.     BuildGrid();
    47.     GUILayout.EndArea();
    48.     print("building grid!");
    49. } // end function OnGUI()
    50.  
    51. function BuildGrid()
    52. {
    53.     GUILayout.BeginVertical(); // begin a vertical control group
    54.     GUILayout.FlexibleSpace(); // center the graphic object by occupying the empty
    55.                                // space that is not filled by the UI controls
    56.    
    57.     for(i=0; i<rows; i++)
    58.     {
    59.         GUILayout.BeginHorizontal(); // begin a horizontal control group
    60.         GUILayout.FlexibleSpace();
    61.        
    62.         for(j=0; j<cols; j++)
    63.         {
    64.             var card:Object = aGrid[i][j];
    65.            
    66.             if(GUILayout.Button(Resources.Load(card.img),GUILayout.Width(cardW)))
    67.             {
    68.                 Debug.Log(card.img);
    69.             } // enf if
    70.         } // end for
    71.        
    72.         GUILayout.FlexibleSpace();
    73.         GUILayout.EndHorizontal(); // end a horizontal control group
    74.     }
    75.    
    76.     GUILayout.FlexibleSpace();
    77.     GUILayout.EndVertical(); // end a vertical control group
    78. } // end function BuildGrid()
    79.  
    80. function BuildDeck()
    81. {
    82.     var totalRobots:int = 4; // we've got 4 robots to work with
    83.     var card:Object; // this stores areference to a card
    84.    
    85.     for(i=0; i<totalRobots; i++)
    86.     {
    87.         var aRobotParts:Array = ["Head", "Arm", "Leg"];
    88.        
    89.         for(j=0; j<2; j++)
    90.         {
    91.             var someNum:int = Random.Range(0, aRobotParts.length);
    92.             var theMissingPart:String = aRobotParts[someNum];
    93.            
    94.             aRobotParts.RemoveAt(someNum); // rips an element out of the
    95.                                            // array at the specified index
    96.            
    97.             card = new Card("robot" + (i+1) + "Missing" + theMissingPart);
    98.             aCards.Add(card);
    99.         } // end for
    100.     } // end for
    101. } // end function BuildDeck()
    102.  
    103. class Card extends System.Object
    104. {
    105.     var isFaceUp:boolean = false;
    106.     var isMatched:boolean = false;
    107.     var img:String;
    108.    
    109.     function Card(img:String)
    110.     {
    111.         this.img = img;
    112.     }
    113. }
    114.  
    Thanks in advance for any help, and remember that once you were also babies who learned to walk :)
     
  41. Zoltar26

    Zoltar26

    Joined:
    Sep 6, 2012
    Posts:
    17
    Ooops... I didn't notice that T_Trojan already answered this issue.
    Thanks anyway.
     
  42. souljasam

    souljasam

    Joined:
    Sep 29, 2012
    Posts:
    1
    So im on chapter 5 page 141. im getting a bunch of compiler errors pretaining to i, j, and BuildGrid() as being unknown identifiers. my code seems to be the same as in the book but when i paste the books code in it does have those issues but line 34 gets 2 issues where it says the = should be a : and its expecting a ; at the lines end when there is one.

    this is my code. bolded areas are where unity says theres an issue.

    #pragma strict

    var cols:int = 4;

    var rows:int = 4;

    var totalCards:int = cols*rows;

    var matchesNeededToWin:int = totalCards*0.5;

    var matchesMade:int = 0;

    var cardW:int = 100;

    var cardH:int = 100;

    var aCards:Array;

    var aGrid:Array;

    var aCardsFlipped:ArrayList;

    var playerCanClick:boolean;

    var playerHasWon:boolean = false;

    function Start()
    {
    playerCanClick = true;
    aCards = new Array();
    aGrid = new Array();
    aCardsFlipped = new ArrayList();

    for(i=0; i<rows; i++)
    {
    aGrid = new Array();
    for(j=0; j<cols; j++)
    {
    aGrid[j] = new Card();
    }
    }
    }

    var customSkin:GUISkin;

    function OnGUI()
    {
    GUILayout.BeginArea(Rect(0,0,Screen.width,Screen.height));
    BuildGrid();
    GUILayout.EndArea();
    print("building grid!");

    }

    class Card extends System.Object
    {
    var isFaceUp:boolean = false;
    var isMatched:boolean = false;
    var img:String;

    function Card()
    {
    img = "robot";
    }
    }
     
  43. Nubz

    Nubz

    Joined:
    Sep 22, 2012
    Posts:
    553
    Total beginner don't even know much about code beyond a basic understanding of what some of it does
    This book is so easy to follow and the author made it fun in a lot of parts(hoagies LOL)
    Thank you for all involved in making it
     
  44. LittleSapper

    LittleSapper

    Joined:
    Oct 22, 2012
    Posts:
    2
    I'm having the same problem. I keep getting errors saying that i and j are unknown identifiers and my code looks the same as above. Any help would be greatly appreciated.

    Thanks!
     
  45. HeartlessPath

    HeartlessPath

    Joined:
    Oct 23, 2012
    Posts:
    3
    SouljaSam LittleSapper

    I too am new to Unity. I have dabbled in Basic, Java C++, but am in no way a master coder of any sorts. I was stuck on this one a bit the other day as well, and to remedy my problem I declared my iterators (i j) OUTSIDE of the loop, (and outside of any other functions), towards the top of the script with the rest of my variables. You may or may not be able to declare the iterators inside the loop, maybe perhaps to the effect of:

    (var i = 0; i<someVariable; i++)

    but I declared mine at the top of my script and mine is working just peachy!

    Hope that helps guys (and/or gals) ^_^
     
    Last edited: Oct 24, 2012
  46. HeartlessPath

    HeartlessPath

    Joined:
    Oct 23, 2012
    Posts:
    3
    For SouljaSam

    If that is your full code for the script, you need to build the actual function for BuildGrid(). If you HAVE already built it, try commenting out the very top line of the script (the #pragma strict part). I think I had that problem as well, and I remembered seeing online that someone suggested to comment out that line (just add the // for comments at the beginning of the line) and then I had smooth running code!

    Hope that helps!
     
  47. LittleSapper

    LittleSapper

    Joined:
    Oct 22, 2012
    Posts:
    2
    Thank you! That solved my problem...now I wish I understood why :p
     
  48. HeartlessPath

    HeartlessPath

    Joined:
    Oct 23, 2012
    Posts:
    3
    Well I'm not 100% sure, but I think it doesn't work that way just because the iterators were never declared at all inside the loop, so they were unknown to the rest of the script. But once you declared them with the rest of the variables, it had a match between a variable named 'i' or 'j' and the instance of the variable that you used within the loop. Maybe I'm right on that explanation, maybe not. Hopefully it might clear things up some, but I'm glad you got the code to work :D
     
  49. PhershOutTheTub

    PhershOutTheTub

    Joined:
    Nov 12, 2012
    Posts:
    5
    Hello. I am on Chapter 5 and can't seem to get rid of these four errors:

    Assets/Scripts/GameScript.js(32,30): BCE0048: Type 'Object' does not support slicing.
    Assets/Scripts/GameScript.js(56,56): BCE0048: Type 'Object' does not support slicing.
    Assets/Scripts/GameScript.js(58,73): BCE0019: 'img' is not a member of 'Object'.
    Assets/Scripts/GameScript.js(60,56): BCE0019: 'img' is not a member of 'Object'.

    Here is my code:

    #pragma strict

    var i:int = 0;
    var j:int = 0;
    var cols:int = 4;
    var rows:int = 4;
    var totalCards:int = 16;
    var matchesNeededToWin:int = totalCards * 0.5;
    var matchesMade:int = 0;
    var cardW:int = 100;
    var cardH:int = 100;
    var aCards:Array;
    var aGrid:Array;
    var aCardsFlipped:ArrayList;
    var playerCanClick:boolean;
    var playerHasWon:boolean = false;

    function Start()
    {
    playerCanClick = true;

    aCards = new Array();
    aGrid = new Array();
    aCardsFlipped = new ArrayList();

    for(i = 0; i < rows; i++)
    {
    aGrid = new Array();

    for(j = 0; j < cols; j++)
    {
    aGrid[j] = new Card();
    }
    }
    }

    function OnGUI()
    {
    GUILayout.BeginArea (Rect (0, 0, Screen.width, Screen.height));
    BuildGrid();

    GUILayout.EndArea();
    print("Building Grid!");
    }

    function BuildGrid()
    {
    GUILayout.BeginVertical();

    for(i = 0; i < rows; i++)
    {
    GUILayout.BeginHorizontal();

    for(j = 0; j < cols; j++)
    {
    var card:Object = aGrid[j];

    if(GUILayout.Button(Resources.Load(card.img), GUILayout.Width(cardW)))
    {
    Debug.Log(card.img);
    }
    }

    GUILayout.EndHorizontal();
    }

    GUILayout.EndVertical();
    }

    class Card extends System.Object
    {
    var isFaceUp:boolean = false;
    var isMatched:boolean = false;
    var img:String;

    function Card()
    {
    img = "robot";
    }
    }

    If anyone can find my error, I would greatly appreciate it! Thanks all!
     
  50. Nubz

    Nubz

    Joined:
    Sep 22, 2012
    Posts:
    553
    My only rant is that the book is in java instead of c#(personal preference not a put down)

    Other than that it was a great start for me when I knew nothing about Unity