- Home /
Question by
pokobros · Feb 25, 2014 at 10:26 PM ·
javascriptobjectreferenceargument
Object reference not set to an instance of an object
I am trying to run this code for a game:
function Start () {
playerCanClick = true; // We should let the player play, don't you think?
// Initialize some empty Collections:
aCards = new List.<Card>(); // this Generic List is our deck of cards. It can only ever hold instances of the Card class.
aGrid = new Card[rows,cols]; // The rows and cols variables help us define the dimensions of this 2D array
aCardsFlipped = new List.<Card>(); // This List will store the two cards the player flips over.
BuildDeck();
// Loop through the total number of rows in our aGrid List:
for(var i:int = 0; i<rows; i++)
{
// For each individual grid row, loop through the total number of columns in the grid:
for(var j:int = 0; j<cols; j++)
{
var someNum:int = Random.Range(0,aCards.Count);
aGrid[i,j] = aCards[someNum];
aCards.RemoveAt(someNum);
}
}
}
function OnGUI ()
{
GUILayout.BeginArea (Rect (0,0,Screen.width,Screen.height));
BuildGrid();
if(playerHasWon)BuildWinPrompt();
GUILayout.EndArea();
}
function BuildGrid()
{
GUILayout.BeginVertical();
GUILayout.FlexibleSpace();
for(var i:int=0; i<rows; i++)
{
GUILayout.BeginHorizontal();
GUILayout.FlexibleSpace();
for(var j:int=0; j<cols; j++)
{
var card:Object = aGrid[i,j];
var img:String;
if(card.isMatched)
{
img = "blank";
} else {
if(card.isFaceUp)
{
img = card.img;
}else{
img = "faceup";
}
}
GUI.enabled = !card.isMatched;
if(GUILayout.Button(Resources.Load(img), GUILayout.Width(80), GUILayout.Height(80)))
{
if(playerCanClick){
FlipCardFaceUp(card);
}
Debug.Log(card.img);
}
}
GUI.enabled = true;
GUILayout.FlexibleSpace();
GUILayout.EndHorizontal();
}
GUILayout.FlexibleSpace();
GUILayout.EndVertical();
}
function BuildDeck(){
var card:Object; // this stores a reference to a card
var id:int = 0;
var num:int = 0;
var otherNum:int = 2;
var aColorParts:List.<String> = new List.<String>();
aColorParts.Add("Green");
aColorParts.Add("Yellow");
aColorParts.Add("Red");
aColorParts.Add("Orange");
aColorParts.Add("Purple");
aColorParts.Add("Pink");
aColorParts.Add("LightBlue");
aColorParts.Add("Blue");
for(var j:int=0; j<2; j++)
{
var theMissingPart:String = aColorParts[num];
aColorParts.RemoveAt(num);
card = new Card("color" + "Missing" + theMissingPart, id);
aCards.Add(card);
card = new Card("color" + theMissingPart, id);
aCards.Add(card);
if(otherNum % 2 == 1){
num++;
}
otherNum++;
id++;
}
}
When I run it, I get an error saying: "Object reference not set to an instance of an object" on this line:
if(card.isMatched)
and an error saying: "Argument is out of range" on this line:
aGrid[i,j] = aCards[someNum];
What is the problem?
Comment
Best Answer
Answer by SinisterRainbow · Feb 25, 2014 at 11:43 PM
You didn't add anything to the aCards list - right now it's empty, and is why argument is out of range. You used 'new' to make memory space for it, but then didn't add anything to it. i.e. you need to add the deck.. from 1...52, aCards.Add(ACARD).
Secondly, you will find Debug.Log("...") a much better friend than you realize. Otherwise posting to the answers every bug issue will take you 17 years to make a small card game. (=