How to use Javascript Array with classes?

Hi , I have checked the unity website for how to use the arrays but still am confused and get errors!

I’m trying to make a simple inventory and currently have a class called item.

class Item{
	var damage : int;
	var name : String;
	var model : GameObject;
	var icon : Texture;
}

However I don’t know how to put this into a array , I am currently using the code below to just get the console to out the name of an item. I also seem to be getting different results depending on whether I select the “maximize on play” button in the editor.

var Inventory : Item[];


function Start () {
Inventory = new Item[10];
Inventory[1].damage = 1;
Inventory[1].name = "hello";
}

function Update () {
print(Inventory[1].name);
}

Any help on this topic , I will be extremely greatful for , Thanks =)

You not only have to create the array (i.e. new Item[10]), but you have to create each individual item that goes into the array. For example:

#pragma strict

class Item{
    var damage : int;
    var name : String;
    var model : GameObject;
    var icon : Texture;
}

var Inventory : Item[] = new Item[10];
 
function Start () {
	Inventory[1] = new Item(); // <<========
	Inventory[1].damage = 1;
	Inventory[1].name = "hello";

	Debug.Log(Inventory[1].name);
}

Note I’ve only create one item for entry index 1. Instead you might want to populate the entire list:

for (var i = 0; i < Inventory.Length; i++) {
    Inventory *= new Item()*

}