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. Tin Htoo Aung

    Tin Htoo Aung

    Joined:
    Nov 20, 2012
    Posts:
    1
    Hi there,

    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.

    here is how I sort that out by help of this answer.

    Hope this will help :)
     
    Last edited: Nov 20, 2012
  2. PhershOutTheTub

    PhershOutTheTub

    Joined:
    Nov 12, 2012
    Posts:
    5
    @Tin Htoo Aung: I tried what they had done, but it had given me more errors than I had originally. I need an alternate solution.
     
  3. Beyond-Ken

    Beyond-Ken

    Joined:
    Dec 19, 2012
    Posts:
    7
    Comment out the '#pragma strict' line:

    //#pragma strict
     
  4. eraser2021999

    eraser2021999

    Joined:
    Dec 19, 2012
    Posts:
    2
    Resource.Load() loads the resources from the game. The resource it's called "robot" (in your folder should be a robot.jpg). Hope this helps
     
  5. eraser2021999

    eraser2021999

    Joined:
    Dec 19, 2012
    Posts:
    2


    try remove #pragma statement if you have any
     
  6. PhershOutTheTub

    PhershOutTheTub

    Joined:
    Nov 12, 2012
    Posts:
    5
    Thanks for the responses, but getting rid of "#pragma strict" does not do anything for me. Sure it'll not say that I have any errors, but when I got to play the game, I click the play button and a blank blue screen comes up. Any help?

    #pragma strict

    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;
    var i:int;
    var j:int;

    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++)
    {
    var someNum:int = Random.Range(0, aCards.length);
    aGrid[j] = aCards[someNum]; //ERROR HERE
    aCards.RemoveAt(someNum);
    }
    }
    }

    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 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), "You've Won!");

    if(GUI.Button (Rect (10, 40, 80, 20), "Play Again!"))
    {
    Application.LoadLevel("Title");
    }

    GUI.EndGroup();
    }

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

    if(playerHasWon) BuildWinPrompt();
    GUILayout.EndArea();
    }

    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]; //ERROR HERE
    var img:String;

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

    GUI.enabled = !card.isMatched; //ERROR HERE

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

    Debug.Log(card.img); //ERROR HERE
    }

    GUI.enabled = true;
    }

    GUILayout.FlexibleSpace();
    GUILayout.EndHorizontal();
    }

    GUILayout.FlexibleSpace();
    GUILayout.EndVertical();
    }

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

    matchesMade ++;

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

    aCardsFlipped = new ArrayList();

    playerCanClick = true;
    }
    }
    }

    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;
    }
    }
     
  7. PhershOutTheTub

    PhershOutTheTub

    Joined:
    Nov 12, 2012
    Posts:
    5
    All error have a comment next to them.
     
  8. getafix99

    getafix99

    Joined:
    Jan 29, 2013
    Posts:
    40
    I'm also doing the book and am on chapter 5 (2nd part of robotRepair)
    I do enjoy this book and very good work by the author, he is informative and funny. there's just the part of the array which collides with #pragma strict
    so I'm trying to avoid using .NET array class and use unity built in array system but it doesn't work for me.

    I would love if next edition could use the unity build array system and maybe a fix for us :)
     
  9. rapcis

    rapcis

    Joined:
    Jan 30, 2013
    Posts:
    1
    Hi I'm new to Unity as well and I am stuck on Robot Repair with difference that my code is working only when building for PC, when I switch over to Android I got errors like:
    BCE0048: Type 'Object' does not support slicing
    or
    BCE0019: 'xxxx' is not a member of 'Object'.
    So if anybody has working code of this chapter for Android and could share it that would be MORE than GREAT..
     
  10. PhershOutTheTub

    PhershOutTheTub

    Joined:
    Nov 12, 2012
    Posts:
    5
    If anyone would have a working code for Robot Repair could you post it here? Thanks!
     
  11. Glurie

    Glurie

    Joined:
    Feb 8, 2013
    Posts:
    14
    Hi there,

    I'm having trouble getting the mousefollow function to work properly. (Chapters 9/10/11, the "break-up"-game as well as the "shoot the moon"-game)
    The problem is that, although the character (man/spaceship) is following the mouse, the character is not at the same X-position as the mouse. Instead the character is offset to the right by a few centimeters....
    Which means I cannot get the character to the far left of the screen.
    (see picture. This is taken with the mouse all the way to the left side of the screen)



    This is the part of the script responsible for the mousefollow function:

    Code (csharp):
    1. var halfW:float = Screen.width / 2;
    2. transform.position.x = (Input.mousePosition.x)/20;
    I have the same issue when using the project downloaded from PacktPub.
    So it is no scripting error.

    I also tried to use the script from the keep-up game (since it seems to be working properly in that game):

    Code (csharp):
    1. var halfW : float = Screen.width/2;
    2. transform.position.x = (Input.mousePosition.x -halfW) /halfW;
    But the result is the same.

    Anyone has any idea why this is happening?
    With my very limited scripting knowledge I cannot think of anything else to try.
    Any help would be much appreciated!

    Regards.
     
    Last edited: Feb 8, 2013
  12. Ajay12

    Ajay12

    Joined:
    Apr 18, 2013
    Posts:
    4
    Hi everyone.I am on page 152 and I am having some problem.Instead of being centered vertically on the screen my grid goes to top right corner. Can anyone help me? This is my script :
    var cols:int = 4;// the number of columns in the card grid
    var rows:int = 4;//the number of rows in the card grid
    var totalCards:int =cols*rows;//I think the answer is 16, but I was never good with numbers
    var matchesNeedToWin:int = totalCards*0.5;//If there are 16 cards, the player needs to find 8 matches to clear the board
    var matchesMade:int = 0;//At the outset, the player has not made any matches
    var cardW:int =100;//Each card's width and height is 100 pixels
    var cardH:int =100;
    var aCards:Array;//We'll store all the cards we create in this Array
    var aGrid :Array;//This array will keep track of the shuffled,dealt cards
    var aCardsFlipped:ArrayList;//This array will store the two cards that the player flips over
    var playerCanClick:boolean;//We'll use this flag to prevent the player from clicking buttons when we don't want him to
    var playerHasWon:boolean = false;//Store whether or not the player has won.This should probably start out false :
    function Start()
    {
    playerCanClick = true;//We should let the player play
    //Initialize the arrays as empty lists:
    aCards = new Array();
    aGrid = new Array();

    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 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";
    }
    }
    function BuildGrid()
    {
    GUILayout.BeginVertical();
    for(i=0;i<rows;i++)
    {
    GUILayout.BeginHorizontal();
    GUILayout.FlexibleSpace();
    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.FlexibleSpace();
    GUILayout.EndVertical();
    }
     
  13. Mark Gravey

    Mark Gravey

    Joined:
    Apr 19, 2013
    Posts:
    2
    Hey Ajay12 it's becuase you put "GUILayout.FlexibleSpace();" under "GUILayout.BeginHorizontal();"
    So your code for function BuildGrid() should look like this.

    function BuildGrid()
    {
    GUILayout.BeginVertical();
    GUILayout.FlexibleSpace();
    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.FlexibleSpace();
    GUILayout.EndVertical();


    This shouldn't really matter as in the next couple pages you add "GUILayout.FlexibleSpace();" to both verticals and horizontals.
     
  14. umlj001

    umlj001

    Joined:
    May 1, 2013
    Posts:
    1
    Hi. Im a beginner. Im reading the beginner guide book on Chapter 5 which is about 'Robot Repair'. It asks me to download the assets. But until now i have no luck finding it on any sites. Can I know where I can download the assets? Thank you
     
  15. Mark Gravey

    Mark Gravey

    Joined:
    Apr 19, 2013
    Posts:
    2
  16. bhontagun

    bhontagun

    Joined:
    May 5, 2013
    Posts:
    1
    kinda stack hear... chapter 5 page 130, where can i get the source package

    i all ready download the paid book, but all i can get is a .htm file and an encrypt code pops up after i open it,


    anyone can share a link? e-mail me
    Kevin_akari@yahoo.com.ph

    thx
     
  17. Zephire

    Zephire

    Joined:
    Apr 6, 2013
    Posts:
    5
    Hi all,

    Fresh to the Unity community, and started on the 'Unity 3D Game Development by Example'. So far I've been enjoying the book a lot and have finished chapter 6 this afternoon.
    Most of the things seem to be working fine except one issue, that despite searching for, I didn't yet seem to find anywhere or solve.

    After finishing the game, when all cards are matched, The box with "You've Won" comes up, the bar with the "Play again" comes up but I cannot click the button ... I've cross checked the final script at the end of the chapter. Sorted all of the code into the same order as the author did. But still, I'm not able to click it. To me it looks like the button is stuck below the cards some way...

    Would be welcome for any ideas or insights on what might be going wrong in this piece of code...

    Thanks in advance!

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

    reign112

    Joined:
    May 30, 2013
    Posts:
    1
    Hi Zephire,

    I was having the same problem(GUIButton not being clickable after winning the game).

    I fixed it by doing the following:

    Code (csharp):
    1. function OnGUI ()
    2.     {      
    3.         GUILayout.BeginArea(Rect (0,0,Screen.width, Screen.height));
    4.        
    5.         GUILayout.BeginHorizontal();
    6.        
    7.         if(playerHasWon)BuildWinPrompt();
    8.        
    9.         BuildGrid();
    10.    
    11.         GUI.EndGroup();
    12.        
    13.         GUILayout.EndHorizontal();
    14.         GUILayout.EndArea();
    15.        
    16.     }
    All I did was move "if(playerHasWon)BuildWinPrompt();" up a few lines so it is read before the "BuildGrid() function".
     
    Last edited: May 31, 2013
  19. Zephire

    Zephire

    Joined:
    Apr 6, 2013
    Posts:
    5
    Hey Reign,

    Thanks for the tip!

    I changed the bit I had going on for the OnGUI function by swapping the lines in place.
    I spotted some additional code in your OnGUI function and added GUILayout.BeginHorizontal(); as well.

    After completing matching all the cards it worked and enabled me to click the button to move back to the title screen.

    Sidenote: while I can now click the button, it does generate the following error while running it in the console.
    I dropped out the GUI.EndGroup(); line as that's part of the end of my BuildWinPrompt function.
    that seemed to fix the error coming up at least.

    Does make me wonder, if you run it do you have a similar error coming up? If not, how would that explain this when I copy the code you're using...

    Looks like we're fiddling about with the same book / tutorial at the moment :) Feel free to drop me a message if you'd fancy connecting up via Skype.
     
  20. MoebiusTeras

    MoebiusTeras

    Joined:
    Jun 2, 2013
    Posts:
    2
    Hello,

    I'm following the book and I got stuck at the end of the chapter 5. I guess I can not make the script right as when I try to play I get en error that states: "All compiler errors have to be fixed before you can enter playmode!". I get this error even if I copy-paste the code taken from some folks who posted their questions before me in this thread. I think the problem is somewhere is Start function (as the problem started when I added that part), but I have no real idea as for what it can be. Can the problem be connected to the fact that the book is based on 3.x software while I'm working on 4.1.3f3 at the moment? Please help. ;( Here is my code:

    Code (csharp):
    1.  
    2. #pragma strict
    3.  
    4. var cols:int = 4; // the number of columns in the card grid
    5. var rows:int = 4; // the number of rows in the card grid
    6. var totalCards:int = 16; // (cols times rows is 16)
    7. var matchesNeededToWin:int = totalCards * 0.5; // If there are 16 cards, the player needs to find 8 matches to clear the board
    8. var matchesMade:int = 0; // At the outset, the player has not made any matches
    9. var cardW:int = 100; // Each card's width and height is 100 pixels
    10. var cardH:int = 100;
    11. var aCards:Array; // We'll store all the cards we create in this Array
    12. var aGrid:Array; // This array will keep track of the shuffled, dealt cards
    13. var aCardsFlipped:ArrayList; // This array will store the two cards that the player flips over
    14. var playerCanClick:boolean; // We'll use this flag to prevent the player from clicking buttons when we don't want him to
    15. var playerHasWon:boolean = false; // Store whether or not the player has won. This should probably start out false :)
    16.  
    17.  
    18.  
    19. function Start () {
    20.     playerCanClick = true;
    21.  
    22.     aCards = new Array();
    23.     aGrid = new Array();
    24.     aCardsFlipped = new ArrayList();
    25.     for(i=0; i<rows; i++)
    26.     {
    27.         aGrid[i] = new Array(); // Create a new, empty array at index i
    28.         for(j=0; j<cols; j++)
    29.         {
    30.             aGrid[i][j] = new Card();
    31.         }
    32.     }
    33. }
    34.  
    35.  
    36.  
    37. var customSkin:GUISkin;
    38.  
    39. function OnGUI () {
    40.     GUILayout.BeginArea (Rect (0,0,Screen.width,Screen.height));
    41.     BuildGrid();
    42.     GUILayout.EndArea();
    43.     print("building grid!");
    44. }
    45.  
    46.  
    47.  
    48. class Card extends System.Object
    49. {
    50.     var isFaceUp:boolean = false;
    51.     var isMatched:boolean = false;
    52.     var img:String;
    53.     function Card()
    54.     {
    55.         img = "robot";
    56.     }
    57. }
    58.  
    59.  
    60.  
    61. function BuildGrid() {
    62.     GUILayout.BeginVertical();
    63.     for(i=0; i<rows; i++)
    64.     {
    65.         GUILayout.BeginHorizontal();
    66.         for(j=0; j<cols; j++)
    67.         {
    68.             var card:Object = aGrid[i][j];
    69.             if(GUILayout.Button(Resources.Load(card.img),
    70.             GUILayout.Width(cardW)))
    71.             {
    72.                 Debug.Log(card.img);
    73.             }
    74.         }
    75.         GUILayout.EndHorizontal();
    76.     }
    77.     GUILayout.EndVertical();
    78. }
     
  21. Zephire

    Zephire

    Joined:
    Apr 6, 2013
    Posts:
    5
    Hi MoebiusTeras,

    I've copied your code into my working version of the chapter 5 game.
    Now the error you're getting is not really handy in identifying what's going wrong. As I'm also completely new to scripting and Unity in general I had to take back the notes I've made while going through the chapter. As I recalled being stuck on a similar issue.

    Try removing the very first line of code:
    Code (csharp):
    1. #pragma strict
    Save and rerun your game. you'll see it will shine bright and glorius as intended :)

    Since I've had the error every single script the book tells me to make I remove all lines in there and start with a clean page.
    Has been helpfull along the way. Still got a bookmark made to check exactl what the
    Code (csharp):
    1. #pragma strict
    does to mess things up, but haven't reached that point yet ;)

    Hopes this helps, let me know if that did it for you or not!
     
  22. MoebiusTeras

    MoebiusTeras

    Joined:
    Jun 2, 2013
    Posts:
    2
    Cheers Zephire, it worked! ;]

    The book didn't mention the "#pragma strict" so I figured it's a standard line that just must be there and I didn't even thought of removing it. Thank you for the solution. ;]
     
  23. Zephire

    Zephire

    Joined:
    Apr 6, 2013
    Posts:
    5
    Ah good to hear :)
    Good luck on the next chapters!
     
  24. Ras

    Ras

    Joined:
    Mar 27, 2013
    Posts:
    3
    I am having the exact same problem. Also, the Inspector displays None(Texture) rather than None(2D Texture)... By the way, I am using Unity 4...
     
  25. Ras

    Ras

    Joined:
    Mar 27, 2013
    Posts:
    3
    It worked for me too!!! Thanks... Moving on....
     
  26. Lanford

    Lanford

    Joined:
    Jun 25, 2013
    Posts:
    1
    Please help me! Here is my code atthe end of chapter 5. When I run, i encounter this error:"ArgumentOutOfRangeException: Index is less than 0 or more than or equal to the list count. Parameter name: index"

    Code (csharp):
    1.  
    2.  
    3. var cols:int =4; // no of columns
    4. var rows:int = 4; // no of rows
    5. var totalCards:int = cols * rows;
    6. var matchesNeededToWin:int  = 8;// 16 cards -> 8 matches to win
    7. var matchesMade:int = 0;
    8. var cardW:int = 100;//Each card's width and height is 100 pixels
    9. var cardH:int = 100;
    10. var aCards:Array; //Stored all cards created
    11. var aGrid:Array;// The Array will keep track of the shuffled, dealt card
    12. var aCardsFlipped:ArrayList;// Stored 2 cards that player flips over
    13. var playerCanClick:boolean;// flag to prevent the player from clicking buttons when we dont want him/her too
    14. var playerHasWon:boolean = false;
    15.  
    16. function Start () {
    17.     playerCanClick = true;
    18.    
    19.     aCards = new Array();
    20.     aGrid = new Array();
    21.     aCardsFlipped = new ArrayList();
    22.    
    23.     for(i = 0; i<rows; i++)
    24.     {
    25.         aGrid[i] = new Array(); //create a new, empty array at index i
    26.        
    27.         for(j=0; j<cols; j++)
    28.         {
    29.         aGrid[i][j] = new Card();
    30.         }
    31.     }
    32.  
    33. }
    34.  
    35. function OnGUI () {
    36.     GUILayout.BeginArea (Rect (0,0,Screen.width,Screen.height));
    37.     BuildGrid();
    38.     GUILayout.EndArea();
    39.     print("building grid!");
    40.  
    41. }
    42.  
    43. class Card extends System.Object
    44. {
    45.     var isFaceUp:boolean = false;
    46.     var isMatched:boolean = false;
    47.     var img:String;
    48.    
    49.     function Card()
    50.     {
    51.     img = "robot";
    52.     }
    53. }
    54.  
    55.  
    56. function BuildGrid()
    57. {
    58.     GUILayout.BeginVertical();
    59.     for(i=0; i<rows; i++)
    60.     {
    61.         GUILayout.BeginHorizontal();
    62.         for(j=0; i<cols; j++)
    63.         {
    64.             var card:Object = aGrid[i][j];
    65.             if(GUILayout.Button(Resources.Load(card.img), GUILayout.Width(cardW)))
    66.             {
    67.                 Debug.Log(card.img);
    68.             }      
    69.         }
    70.         GUILayout.EndHorizontal();
    71.     }
    72.     GUILayout.EndVertical();
    73.    
    74. }
    75.  
    76.  
     
  27. Spyryt

    Spyryt

    Joined:
    May 22, 2014
    Posts:
    1
    I'm currently working through Unity 4x Game Development by Example: Beginner's Guide and have hit a problem with my code.

    Code (csharp):
    1. #pragma strict
    2. import System.Collections.Generic;
    3.  
    4. var cols:int = 4; // the number of columns
    5. var rows:int = 4; // the number of rows
    6. var totalCards:int = 16; // number of cards
    7. var matchesNeededToWin:int = totalCards * 0.5;
    8. var matchesMade:int = 0;
    9. var cardW:int = 100;
    10. var aCards:List.<Card>;
    11. var aGrid:Card[,];
    12. var aCardsFlipped:List.<Card>;
    13. var playerCanClick:boolean;
    14. var playerHasWon:boolean = false;
    15.  
    16. function Start ()
    17. {
    18.     playerCanClick = true;
    19.    
    20.     aCards = new List.<Card>();
    21.     aGrid = new Card[rows, cols];
    22.     aCardsFlipped = new List.<Card>();
    23.     for(var i:int = 0; i < rows; i++)
    24.     {
    25.         for(var j:int = 0; j < cols; j++)
    26.         {
    27.             var someNum:int = Random.Range(0, aCards.Count);
    28.             aGrid[i,j] = aCards[someNum];
    29.             aCards.RemoveAt(someNum);
    30.         }
    31.     }
    32.     BuildDeck();
    33. }
    34.  
    35. class Card extends System.Object
    36. {
    37.     var isFaceUp:boolean = false;
    38.     var isMatched:boolean = false;
    39.     var img:String;
    40.    
    41.     function Card(img:String)
    42.     {
    43.         this.img = img;
    44.     }
    45. }
    46.  
    47.  
    48. function OnGUI ()
    49. {
    50.  
    51.  
    52.     GUILayout.BeginArea (Rect(0, 0, Screen.width, Screen.height));
    53.     BuildGrid();
    54.     GUILayout.EndArea();
    55.     print("building grid!");
    56. }
    57.  
    58.  
    59. function BuildDeck()
    60. {
    61.     var totalRobots:int = 4;
    62.     var card:Card;
    63.     for(var i:int=0; i<totalRobots; i++)
    64.     {
    65.         var aRobotParts:List.<String> = new List.<String>();
    66.         aRobotParts.Add("Head");
    67.         aRobotParts.Add("Arm");
    68.         aRobotParts.Add("Leg");
    69.         for(var j:int=0; j<2; j++)
    70.             {
    71.                 var someNum:int = Random.Range(0, aRobotParts.Count);
    72.                 var theMissingPart:String = aRobotParts[someNum];
    73.                 aRobotParts.RemoveAt(someNum);
    74.                 card = new Card("robot" + (i+1) + "Missing" + theMissingPart);
    75.                 aCards.Add(card);
    76.                 card= new Card("robot" + (i+1) + theMissingPart);
    77.                 aCards.Add(card);
    78.             }
    79.     }
    80. }
    81.  
    82.  
    83. function BuildGrid()
    84. {
    85.     GUILayout.BeginVertical();
    86.     GUILayout.FlexibleSpace();
    87.     for(var i:int = 0; i < rows; i++)
    88.     {
    89.         GUILayout.BeginHorizontal();
    90.         GUILayout.FlexibleSpace();
    91.         for(var j:int = 0; j < cols; j++)
    92.         {
    93.             var card:Card = aGrid[i,j];
    94.             if(GUILayout.Button(Resources.Load(card.img), GUILayout.Width(cardW)))
    95.             {
    96.                 Debug.Log(card.img);
    97.             }
    98.         }
    99.         GUILayout.FlexibleSpace();
    100.         GUILayout.EndHorizontal();
    101.     }
    102.     GUILayout.FlexibleSpace();
    103.     GUILayout.EndVertical();
    104. }
    I feel like my code is exactly like it is in the book (up to page 201) and yet I get the following error message when I try to run the game in Unity:

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


    Could anyone assist please? Thanks in advance.
     
    Last edited: May 22, 2014
  28. stephenjohnellis

    stephenjohnellis

    Joined:
    Jul 15, 2014
    Posts:
    2
    Hey All,

    I've worked my way successfully through the book and managed to get all of the games functional. I'm in the process of going back through and customizing each one but as this is my first introduction to programming with javascript, I'm running into a couple of issues.

    Mainly, I'm working on a re-skin of the Robot Repair memory game and I'd like to set up a custom size for the array. Ideally, I'd like to change the size of the grid for each new level but I can only seem to get the 4x4 grid to work. When I change the initial variables:

    var cols:int = 3;
    var rows:int = 3;

    It still draws a 4x4 grid. I'm not sure what I'm missing.

    How would I go about setting up a grid that 2x4 or 2x7?

    Thanks!
     
    Last edited: Jul 15, 2014
  29. stephenjohnellis

    stephenjohnellis

    Joined:
    Jul 15, 2014
    Posts:
    2
    New Question, was hoping someone could answer this. I'm work with the memory game and rather than have the same image on all the card backs, I'd like to create a mosaic using multiple images. Given the buildgrid function in the book, how would I best go about tweaking that to assign separate textures to each of the grid location the layout creates? Below is the code I have and attached is an image example of what I am trying to accomplish for the back of the cards.

    Thanks!

    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:Card = aGrid[i,j];
    var img:String;

    if(card.isMatched)
    {
    img = "back";
    }else{

    if(card.isFaceUp)
    {
    img = card.img;
    }else{
    img = "Set1_1(0_0)";
    }
    }
    if(GUILayout.Button(Resources.Load(img),GUILayout.Width(cardW),GUILayout.Height(cardH)))
    {
    if(playerCanClick)
    {
    FlipCardFaceUp(card);
    }
    Debug.Log(card.img);
    }
    }
    GUI.enabled = true;
    GUILayout.FlexibleSpace();
    GUILayout.EndHorizontal();
    }
    GUILayout.FlexibleSpace();
    GUILayout.EndVertical();
    }
     

    Attached Files:

  30. jack53441

    jack53441

    Joined:
    Sep 5, 2015
    Posts:
    5
    Attention to Beginner,

    I have just bought the book Unity 3D Development by Example. - Ryan Henson.
    So far i have gone to Chapter 5. - a bumpy ride indeed.

    Before you start using the book, my advice is to use older Unity version before 4.6
    This is because the book is based on Legacy GUI which was replaced by new UI
    in Unity 4.6. If you are using newer version, you may problem creating things like
    GUI Texture in the menu itself.

    In Chapter 5, remove the #pragma strict statement in the beginning, so
    the multidimensional array aGrid is recognized by the compiler.
    aGrid[j] = new Card();

    Finally, read the errata to ensure you got the latest changes in the book!
    See page 6 for the link.

    I hope i can progress to more Chapters and share with you what awaiting...
     
  31. jack53441

    jack53441

    Joined:
    Sep 5, 2015
    Posts:
    5
    Hi Beginners PART 2 about Chapter 6,

    For Chapter 6, you need to pay attention to 4 things here :

    1) page 161, you'll notice that

    for (var j:int = 0; j<cols; j++)

    where the j is declared as int.
    It is okay to declare this way, it still works the same.
    Anyway var should be declared this way for readability.

    2) page 174 - there are changes to the code but not highlighted in BOLD :
    Don't overlook.

    if (playerCanClick) FlipCardFaceUp(card);

    3) Also the statement GUI.enabled = true;

    should be outisde another } like this :

    Debug.Log(card.img)
    }
    }
    GUI.enabled = true;

    Otherwise the Play Again button won't be activated.
    Please note that don't add another } , you will get compilation error.
    Simply move the statement below another }.

    4) Finally page 176, you notice that print "building grid!" is not there.

    You should delete this line otherwise you won't be able to see the Log message
    as it keeps overwrite whatever you clicks.

    That's all for chapter 6.
     
  32. jack53441

    jack53441

    Joined:
    Sep 5, 2015
    Posts:
    5
    Hi Spyryt,

    I hope you already fix this.

    At first glance you need to execute BuildDeck before assigning to multidimensional aGrid array.
    Try put the BuildDeck at the begining of Start function. Hope this works.
     
  33. jack53441

    jack53441

    Joined:
    Sep 5, 2015
    Posts:
    5
    Hi Folks, - about Chapter 7

    1) If you can't load the material page 185, you still can proceed without much worrying.
    Just skip step 7 & 12.

    2) If you want to decrease the graphic bar from left to right (opposite way), use this
    statement instead :

    GUI.BeginGroup(new Rect(clockFG.width-newBarWidth+5,6,newBarWidth, clockFG.height));

    3) Errata Page 203, the value of rot should be zero when the clock times out (not 360).

    4) Another Way to understand how the rotation degree to be calculated is as follow (page 205):
    Please note there is no error in the codes, i just give alternative way to accomplish the same thing.

    The value rot is decreasing from 360 to 0, while the rotation needed by the rightSide Texture is from
    zero to 360. In short, the degree of rotation needed at given time is (360-rot).

    GUIUtility.RotateAroundPivot(360-rot, centerPoint);

    Similarly, after PastHalfWay, the degree is (360 - rot - 180).
    The additional minus 180 is needed because we know the leftSide already by default rotated 180 degree
    in advance everytime we will draw them.

    GUIUtility.RotateAroundPivot(360-rot-180, centerPoint);

    Both statements work the same.
     
  34. Unitier503

    Unitier503

    Joined:
    Sep 12, 2015
    Posts:
    2
    I had a problem with the chapter 8, I follow the code, create tag "tray" but when Play the GUI text didn't count help me!!!
    Code (JavaScript):
    1. #pragma strict
    2. var hitCount:GUIText;
    3. var numHits:int = 0;
    4. var bestScore:int = 0;
    5. var lastBest:int = 0;
    6. var hasLost:boolean = false;
    7. var velocityWasStored:boolean = false;
    8. var storedVelocity:Vector3;
    9. function Start () {
    10.  
    11. }
    12. function OnCollisionEnter(col : Collision){
    13.     if(col.gameObject.tag == "tray"){
    14.         //Debug.Log("Yes! hit tray!");
    15.         if(!velocityWasStored){
    16.             storedVelocity = GetComponent.<Rigidbody>().velocity;
    17.             velocityWasStored = true;
    18.             }
    19.         if(GetComponent.<Rigidbody>().velocity.y > 1){
    20.                 numHits++;
    21.             }
    22.     GetComponent.<Rigidbody>().velocity.y = storedVelocity.y;
    23.     }
    24. }
    25. function Update () {
    26.     var str:String = "";
    27.     if (!hasLost){
    28.         str = numHits.ToString();
    29.     }
    30.     else{
    31.         str = "Hits: " +numHits.ToString()+"\nYour best: "+bestScore;
    32.         if (bestScore > lastBest)
    33.             str += "\nNEW RECORD!";
    34.     }
    35.     hitCount.text = str;
    36.     if(transform.position.y < -3){
    37.         if(!hasLost){
    38.             hasLost =true;
    39.             lastBest = bestScore;
    40.             if(numHits > bestScore){
    41.                 bestScore = numHits;
    42.             }
    43.         }
    44.     }
    45. }
    46. function OnGUI(){
    47.     if(hasLost){
    48.         var buttonW:int = 100;
    49.         var buttonH:int = 50;
    50.         var halfScreenW:float = Screen.width/2;
    51.         var halfButtonW:float = buttonW/2;
    52.         if(GUI.Button(Rect(halfScreenW - halfButtonW, Screen.height*.8,buttonW,buttonH),"Play Again")){
    53.             numHits = 0;
    54.             hasLost = false;
    55.             transform.position = Vector3(0.5,2,-0.05);
    56.             GetComponent.<Rigidbody>().velocity = Vector3(0,0,0);
    57.         }
    58.     }
    59. }
     
  35. jack53441

    jack53441

    Joined:
    Sep 5, 2015
    Posts:
    5
    Hi,

    I tested your code replacing mine, it works the same.

    Can it be the setting on the inspector panel ? Tie your Hit Count to Bounce Count.

    upload_2015-9-17_23-44-33.png






     
  36. Unitier503

    Unitier503

    Joined:
    Sep 12, 2015
    Posts:
    2
    thanks for ur reply but i already check it...it still there...GUI Text Bounce Count is dragged to Hit Count....and when i play...It still "0"