NullReferenceException: Object reference not set to an instance of an object

Hello!

I’m currently getting the following error:

NullReferenceException: Object reference not set to an instance of an object GameTracker_Script.LoadItems()(at Assets/Scripts/GameTracker_Script.js:79) GameTracker_Script.Start() (at Assets/Scripts/GameTracker_Script.js:32)

What I am trying to do is create an array of objects from a class called “Items” and access these from the method LoadItems() and fill in two variables in each object. The error occurs on the first line in the LoadItems() method.

Here’s the class I am making an array of:

class Items
{

var objectName : String;
var uses : boolean[] = new boolean[3];


function SetName (name : String)
{
	objectName = name;
}


function SetUses (speak : boolean, inspect : boolean, pickup : boolean)
{
	uses[0] = inspect;
	uses[1] = speak;
	uses[2] = pickup;
}

}

Here I create the array and call the function for filling it:

var itemList : Items[];

function Start ()
{
	itemList = new Items[5];
	LoadItems();
}

And this is where I get the error:

function LoadItems ()
{
	itemList[0].SetName("BoxEn");
	itemList[0].SetUses(true, true, true);

	itemList[1].SetName("BoxTo");
	itemList[1].SetUses(false, true, true);
	
	itemList[2].SetName("BoxTre");
	itemList[2].SetUses(true, false, true);
	
	itemList[3].SetName("BoxFire");
	itemList[3].SetUses(false, false, true);
}

Anyone knows what I am doing wrong?

Regards, Mr Gobblez.

You need to initialize the items in the array when they aren’t primitives.

function Start ()
{
    itemList = new Items[5];
    for(var i = 0; i < 5; i++)
        itemList *= new Items();*

LoadItems();
}