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