- Home /
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;
}
}
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..
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
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?
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++;
}
}
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)?
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.
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
Follow this Question
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