Array error (Argument Is Out Of Range)

Hey all,

I’m new to Unity and new to programming. I’m coming from an art background as a 3D artist, and as you can imagine, programming is not coming easy! However, I’m keen to start attempting some game ideas.

I’m working through a book called ‘Unity by Example’ and the current project covers a flip/match memory card game. I’m running into an Argument is out of Range error, Index is less than 0 or more than or equal to the list count.

Now, to me, arrays are the hardest concept to grasp thus far in my programming experience, but I get how they work. However, I’m having problems figuring out just what is causing the error. If anyone could have a quick look through the code and possibly suggest what I’ve done wrong, it’d be much appreciated!

Thanks

var customSkin:GUISkin;

var cols:int = 4;
var rows:int = 4;
var totalCards:int = cols*rows;
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;

function Start() {
	playerCanClick = true;
	aCards = new Array();
	aGrid = new Array();
	aCardsFlipped = new ArrayList();
	
	for (i=0; i<rows; i++){
		aGrid[i] = new Array();
	}
	
	for (j=0; j<cols; j++){
		aGrid[i] [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();
	for(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.EndHorizontal();
}
GUILayout.EndVertical();

}

In the Start() you probably want the j loop inside the i loop. Right now you have them running one after the other. You just need to move the closing } down a few line. A very common mistake.

for (i=0; i<rows; i++) {
	aGrid[i] = new Array();
	for (j=0; j<cols; j++){
	     aGrid[i] [j] = new Card();
	}
 }

Thanks a lot Pakfront, that sorted it.

So what does the error actually mean? I understand why the second array should be created inside of the first, but I don’t understand what that error means in reference to it.

Thanks again

The error means you’re trying to access a particular array index that doesn’t exist.

If you create an array with 4 objects in it, you can access them by using the indices 0, 1, 2, and 3. If you try any index other than those (-1, -2, 4, 5, 10000) then that error is thrown. How could access item -2? Or item 100 in an array that only has 4 items?

And the reason it happened was you only filled slot j=0 int the start(), but then later you tried to access j=1,2,3…

Thanks guys.

I’m finding nested arrays to be incredibly tricky to grasp.

I get this:

for (i=0; i<rows; i++){
        aGrid[i] = new Array();
    }

It’s basically saying count ‘i’ from 0-3 creating 4 arrays in aGrid (0,1,2,3)

But then I don’t get the next bit:

for (j=0; j<cols; j++){
        aGrid[i] [j] = new Card();
        }

Specifically ‘aGrid*[j]’*
Is it inheriting the ‘i’ value from the first ‘for’ and saying ‘create 4 more slots in each new aGrid array’ so it would go:
aGrid 1 + add 0,1,2,3
aGrid 2 + add 0,1,2,3
aGrid 3 etc etc
Or am I missing something?

That’s it exactly. Another way of rewriting the logic (maybe it will make more sense this way):

for (rowNumber=0; rowNumber<rows; rowNumber++) 
{
	var cardRow = new Array();
	
	for (cardNumber=0; cardNumber<cols; cardNumber++)
	{
	     	cardRow[cardNumber] = new Card();
	}
	
	aGrid[rowNumber] = cardRow;
 }

Cheers Fizix, makes perfect sense!