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

Print the value of humanNum right before

var human:int = aHuman[humanNum];

And figure out why it isn’t valid…

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)?

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… :wink:

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*==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++;*
    
  • }*
    }