Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Leon.S.Kennedy · Sep 15, 2012 at 07:53 PM · rangeargumentoutof

ArgumentOutOfRange Exception

Hi guys, I bought the book Unity Game Development by Example Beginner's Guide. And in chapter 5, Making a robot repair game, i keep getting this error

ArgumentOutOfRangeException: Index is less than 0 or more than or equal to the list count. The error is from 12th line inside Start() Function just below the Random.Range code

I dont know how to solve this issue, Please help...

Here is the code..

 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 matchesNeededToWin: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,    don't you think?
     // Initialize the arrays as empty lists:
     aCards = new Array();
     aGrid = new Array();
     aCardsFlipped = new ArrayList();
     for(var i=0; i<rows; i++)
     {
         aGrid[i] = new Array(); // Create a new, empty array at index i
         for(var j=0; j<cols; j++)
         {
             var someNum:int = Random.Range(0,aCards.length);
             aGrid[i][j] = aCards[someNum];
             aCards.RemoveAt(someNum);
         }
     }
     BuildDeck();
 }
 
 class Card extends System.Object
 {
     var isFaceUp:boolean = false;
     var isMatched:boolean = false;
     var img:String;
     function Card(img:String)
     {
         this.img = img;
     }
 }
 
 function OnGUI ()
 {
     GUILayout.BeginArea (Rect (0,0,Screen.width,Screen.height));
     BuildGrid();
     GUILayout.EndArea();
     print("building grid!");
     
 }
 
 function BuildGrid()
 {
     GUILayout.BeginVertical();
     GUILayout.FlexibleSpace();
     for(var i=0; i<rows; i++)
     {
         GUILayout.BeginHorizontal();
         GUILayout.FlexibleSpace();
         for(var j=0; j<cols; j++)
         {
             var card:Object = aGrid[i][j];
             if(GUILayout.Button(Resources.Load(card.img),GUILayout.Width(cardW)))
             {
                 Debug.Log(card.img);
             }
         }
         GUILayout.FlexibleSpace();
         GUILayout.EndHorizontal();
     }
     GUILayout.FlexibleSpace();
     GUILayout.EndVertical();
 }
 
 function BuildDeck() {
     
     var totalRobots:int = 4; // we've got four robots to work with
     var card:Object; // this stores a reference to a card
     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);
             aCards.Add(card);
             card = new Card("robot" + (i+1) + theMissingPart);
             aCards.Add(card);
         }
     }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Graham-Dunnett · Sep 15, 2012 at 09:40 PM

Are you meant to call BuildDeck() earlier? The line with the error is :

 aGrid[i][j] = aCards[someNum];

and I cannot see where the array aCards get initialised. Your BuildDeck() appears to do this, but you call this function later in the code. So, when you generate the random someNum and then access that entry in the aCards array, the program stops with the error that the aCards array doesn't have a someNum entry.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Leon.S.Kennedy · Sep 16, 2012 at 03:02 AM 0
Share

Thank u very much Graham, I moved the builddeck() function call on top of the for loop and it works fine now... Thanks Again...

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Array index is out of Range!? 1 Answer

Random object, Array index out of range 0 Answers

Simple index out of range question? 1 Answer

Wierd Animation Bug 0 Answers

Simple index out of range question? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges