Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Freich · Jun 14, 2011 at 06:34 PM · out of range

Argument is out of range. Parameter name: index

I have a tough problem for some days now. Therefore I made a test project with only the needed code for the problem. Everyting works fine when I run the programm except sometimes. Normally I get this in my console:

 Up:id1
 UnityEngine.Debug:Log(Object)
 GameScript:BuildDeck() (at Assets/Scripts/GameScript.js:69)
 GameScript:Start() (at Assets/Scripts/GameScript.js:26)
 Down:id1
 UnityEngine.Debug:Log(Object)
 GameScript:BuildDeck() (at Assets/Scripts/GameScript.js:69)
 GameScript:Start() (at Assets/Scripts/GameScript.js:26)

And then down till id8.

At the bottom I have:

 test7
 UnityEngine.Debug:Log(Object)
 GameScript:BuildDeck() (at Assets/Scripts/GameScript.js:75)
 GameScript:Start() (at Assets/Scripts/GameScript.js:26)

But the problem lies at the piece of code generating that. Sometimes I get the error: ArgumentOutOfRangeException: Argument is out of range. Parameter name: index System.Collections.Generic.List`1[System.Int32].get_Item (Int32 index) GameScript.BuildDeck () (at Assets/Scripts/GameScript.js:74) GameScript.Start () (at Assets/Scripts/GameScript.js:26)

I really don't know why I get that sometimes. Read alot on wrong arrays etc. but as far as I know everything is ok.

I use the generic List instead of ArrayList, because it's better. And I need a list for my contain method.

My code is:

 import System.Collections.Generic;
 
 var cols:int = 4; // aantal kolommen
 var rows:int = 4; // aantal rijen
 var totalCards:int = cols*rows; //totaal kaarten
 var matchesNeededToWin:int = totalCards * 0.5; //benodigde paren
 var matchesMade:int = 0; //startscore
 var cardW:int = 100;
 var cardH:int = 100;
 var aCards:Array; //Card array
 var aGrid:Array; // dealt card array
 var aCardsFlipped:ArrayList; //flipped cards array
 var aHuman:List.<int>;
 var playerCanClick:boolean;
 var playerHasWon:boolean = false;
 
 function Start ()
 {
     playerCanClick = true;
     
     // Initialize the arrays:
     aCards = new Array();
     aGrid = new Array();
     aCardsFlipped = new ArrayList();
     aHuman = new List.<int>();
     BuildDeck();
 }
 
 function BuildDeck ()
 {
     var totalHumans:int = 8; // 8 needed humans (16 humans total)
     var card:Object; // reference to a card
     var id:int = 0;
     
     while (aHuman.Count < totalHumans)
     {
         var humanNum:int = Random.Range(1,17); // random humans 1 t/m 16
         if (!aHuman.Contains(humanNum))
         {
             aHuman.Add(humanNum);
         }
     }
     
     /*
     for (var humanNum in aHuman)
     {
         Debug.Log("human:"+humanNum); //print List aHuman
     }
     */
     
     
     
     for (k=0; k<totalHumans; k++)
     {
         var aHumanParts:Array = ["Up", "Down"]; // 2 human parts
         id++;
         
         for (l=0; l<2; l++)
         {
             var someNum:int = Random.Range(0, aHumanParts.length);
             var theMissingPart:String = aHumanParts[someNum];
             
             aHumanParts.RemoveAt(someNum);
             
             card = new Card("human", theMissingPart, id);
             //card = new Card("human", humanNum, theMissingPart, id);
             aCards.Add(card);
             //Debug.Log("human", humanNum, card.img+":id"+id);
             Debug.Log(card.img+":id"+id);
             //Debug.Log("Part:"+theMissingPart);
         }
         //aHuman.RemoveAt(humanNum);
     }
     var human:int = aHuman[humanNum];
     Debug.Log("test"+human);
 }
 
 class Card extends System.Object
 {
     var isFaceUp:boolean = false;
     var isMatched:boolean = false;
     var img:String;
     var id:int;
     var human:String;
     
     function Card(human:String, img:String, id:int)
     {
         this.human = human;
         this.img = img;
         this.id = id;
     }
 }
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

3 Replies

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

Answer by flaviusxvii · Jun 14, 2011 at 07:07 PM

Print the value of humanNum right before

 var human:int = aHuman[humanNum];

And figure out why it isn't valid..

Comment
Add comment · Show 4 · 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 Freich · Jun 15, 2011 at 07:36 AM 0
Share

I did what you said but nothing changes. I see my unique list of 8 humans printed now, but I only had that as a check.

Doesn't solve why var human:int = aHuman[humanNum]; Debug.Log("test"+human); works half of the time, and the other half it gives the error: ArgumentOutOfRangeException: Argument is out of range. Parameter name: index

avatar image flaviusxvii · Jun 15, 2011 at 03:11 PM 0
Share

ArgumentOutOfRangeException means you're trying to use an invalid index in the array. If you print the value of humanNum you can see what that invalid index is, then find out why you are trying to use it.

Get it?

avatar image Freich · Jun 15, 2011 at 03:20 PM 0
Share

I get it, thank you very much!

avatar image flaviusxvii · Jun 15, 2011 at 03:25 PM 0
Share

Don't forget to mark / vote answers..

avatar image
0
Best Answer

Answer by Freich · Jun 15, 2011 at 03:28 PM

Thanks for the help all. I was busy the whole day with it but now I got it working. I used a different approach though. Note that I'm new to programming and this was a hard cookie to crumble for me.. ;)

For anyone interested here is my new function:

 function BuildDeck ()
 {
     var card:Object; // reference to a card
     var id:int = 0;
     var z:int=0;
     var aHumanNum = new Array(); // generate 8 unique humans
     while(aHumanNum.length < 8)
     {
         var randomnumber:int = Random.Range(1, 17);
         var found:boolean = false;
         
         for(var i=0; i<aHumanNum.length; i++)
         {
             if(aHumanNum[i]==randomnumber)
             {
                 found=true;
                 break;
             }
         }
         if(!found)aHumanNum[aHumanNum.length]=randomnumber;
     }
     
     
     for (k=0; k<8; k++)
     {
         var aHumanParts:Array = ["Up", "Down"]; // 2 human parts
         id++;
         
         card = new Card("human"+(aHumanNum[0+z]) + "Up", id);
         aCards.Add(card);
         //Debug.Log(card.img+":id"+id);
         card = new Card("human"+(aHumanNum[0+z]) + "Down", id);
         aCards.Add(card);
         //Debug.Log(card.img+":id"+id);
         z++;
         
     }
 }
Comment
Add comment · 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
0

Answer by almo · Jun 14, 2011 at 07:05 PM

 var humanNum:int = Random.Range(1,17);

I thought you only had 8 humans. Don't you mean Range(0, 7)?

Or, since your comment says something about 16 humans, Rand(0, 15)?

Comment
Add comment · Show 2 · 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 Freich · Jun 15, 2011 at 07:19 AM 0
Share

Yes I have 16 human images ranged from 1 - 16. But I only want to use 8 of them every new game. That's why I have a list of 16, but have a random list with 8 unique humans.

avatar image Naivion · Jan 06, 2017 at 07:44 PM 0
Share

Very old post... but i felt like mentioning that Random.Range ignore the last int... so Random.Range(0,7); will output the numbers 0-6... the last one is never called...

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

whats this Argument Out Of Range error when navigating between 0 and max value? 1 Answer

Errors in Editor after Built & Run 0 Answers

Prefab throwing out of range exception when dragged into Hierarchy from Project Folder 0 Answers

Trouble with for loop out of range. Simple? Maybe. 1 Answer

Errors in Editor after Built&Run 0 Answers


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