Inventory problem

Hi.

I Got a problem with my inventory script. I have to scripts at the moment:

  • Lootable Object
  • Inventory

This is the content of both scripts:

//	Inventory.js
//	This script manages the inventory and displays the inventory items in a grid pattern
//	Attach to your Inventory Manager Object
//	Based on the code posted by Der Dude on the Unity3D forums: http://forum.unity3d.com/viewtopic.php?t=11865

static var statInventory : Inventory1;										//	To set an instance of this script
enum SlotType 	{Items, Weapons, Logs, Empty}

//	HELPER CLASSES
@System.Serializable														//	Our Representation of an InventoryItem
class InventoryItem 
{
	var itemName : String;													//	What the item will be called in the inventory
	var itemIcon : Texture;													//	What the item will look like in the inventory
	var itemDescription : String;											//	The description of the item
	//var slotType : SlotType;												//	What slot the item will fit in
}


private var inventory : InventoryItem[];									//	Our master inventory (private)							
private var contentArray : InventoryItem[];									//	The array to contain the item being passed to and from the LootObject

var inventoryWidth : int;													//	the number of columns to display the inventory in
var inventoryLength : int;													//	the size of the inventory in total number of slots

var iconWidthHeight : int;													//	The pixel size (height and width) of an inventory slot
var spacing : int;															//	Space between slots (in x and y)
var offSet : Vector2;														//	The start position of the inventory

var emptySlot : Texture;													//	This will be drawn when a slot is empty

private var openInventoryWindow : boolean;									//	Controls OnGUI and opens/closes the inventory window
								
private var inventoryWindow : Rect;											//	The dimensions of the inventory window

private var currentLootableItem : LootObject;								//	The pointer to the current lootable item being processed
private var newLootableItem : LootObject;									//	The pointer to a new lootable item to be processed

function Awake () 
{																			//	Setup the initial states of the variables
	statInventory = this;
	
	inventoryWindow = new Rect (10, 10, 100, 100);

	openInventoryWindow = false;

	currentLootableItem = null;
	
	inventory = new Array (inventoryLength);								//   Create  init the array to hold the inventory 
	for (var i : int = 0; i < inventory.length; i++) 
	{ 
		inventory[i] = null; 
	}
}

function Update () 
{				
	if (Input.GetKeyUp (KeyCode.I)) 
	{																					//	If the "i" key is pressed...
		openInventoryWindow = !openInventoryWindow;										//	... toggle the inventory window.
		
	}
}

function OnGUI () 
{
	//	Inventory Window
	if (openInventoryWindow) 												//   If the "open inventory window" toggle is true
	{																	
		GUI.Window (1, inventoryWindow, DrawInventoryWindow, "Inventory");	//   The title of this window could be passed as a String from the LootableItem
	}
}


function DrawInventoryWindow () 				//	The window function to draw the inventory window
{											
	if (GUI.Button (Rect (5,5,10,10), "")) 		// Left upper corner quit button
	{
		CloseInventoryWindow ();
	}
	
	var j : int;
	var k : int;
	var currentInventoryItem : InventoryItem;								//   Establish a variable to hold our data
	for (var i : int = 0; i < inventory.length; i ++) {						//   Go through each row ...
		j = i / inventoryWidth;												//   ... divide by array by width to get rows...
		k = i % inventoryWidth;												//   ... find the remainder by width to get columns...
		currentInventoryItem = inventory[i];								//   ... set this point in the matrix as our current point ...
		currentRect = (new Rect (offSet.x + k * (iconWidthHeight + spacing), offSet.y + j * (iconWidthHeight + spacing), iconWidthHeight, iconWidthHeight));
		if (currentInventoryItem == null) 				//   ... if there is no item in the j-th row and the k-th column, draw a blank texture
		{									
			GUI.DrawTexture (currentRect, emptySlot);
		} 
		else 
		{
			GUI.DrawTexture (currentRect, currentInventoryItem.itemIcon);
		}

		//   If there is an item at this location and there is a button click...
		if (currentInventoryItem != null  GUI.Button (currentRect, "", GUIStyle ("label"))) 
		{
			if (Input.GetMouseButtonUp (0)) 								//   ... if that click is mouse button 0: see the description
			{																
				GUIContent ("     " + currentInventoryItem.itemDescription);  				// Get the description out
			} 
			
			
		}
	}
}


function CloseInventoryWindow () 
{
	openInventoryWindow = false;
}

function AddItem (item : InventoryItem) 
{
	for (var i : int = 0; i < inventory.length; i ++) 					//	Go through each row
	{																	
		if (inventory[i] == null) 										//	If the position is empty..
		{				
			inventory[i] = item;										//	... add the new item....
			return (true);												//	... and exit the function.
		}
	}
	Debug.Log ("Inventory is full");
	return (false);
}


function ResizeInventory (newInventoryLength) 				//	This code is never called at this point, but can be when you integrate it.
{							
	var oldInventory : InventoryItem[] = inventory;
	
	inventory = new Array (newInventoryLength);
	for (var i : int = 0; i < oldInventory.length; i++) 
	{ 
		inventory[i] = oldInventory[i];
	}
	
	for (var j : int = oldInventory.length; j < inventory.length; j++) 
	{ 
		inventory[i] = null;
	}
}

Lootable Object:

//	LootableObject.js
//	Make the GameObject "Lootable".
//	This script will 
//	Attach this script to any GameObject that is "Lootable"

#pragma strict

private var theInventoryItem : InventoryItem;										//	The current contents of this lootable object
private var thisLootableItem : LootObject;										//	A reference to this instance of this script
private var clicked : boolean;													//	Trap for creating the current contents only once
private var canLoot : boolean;													//	Boolean to control TestDistance.
private var playerTransform : Transform;										//	A reference to the Player Transform
private var thisTransform : Transform;

var itemName : String;
var itemIcon: Texture2D;
var itemDescription : String;


private var Inventory : Inventory1;


function Start () 
{																			//	Setup the initial states of the variables
	//Init the inventory
	Inventory = new Inventory1();
	
	//itemName = the object name
	itemName = this.gameObject.name;
	//itemIcon will get selected in Unity
	//itemDescription as well
	
	theInventoryItem = new InventoryItem();
	//Init thhe inventory item
	theInventoryItem.itemName = itemName;
	theInventoryItem.itemIcon = itemIcon;
	theInventoryItem.itemDescription = itemDescription;
	
	thisTransform = this.transform;
	playerTransform = GameObject.FindWithTag("Player").transform; 
	thisLootableItem = this;
	clicked = false;
	
}


function OnMouseDown() 
{																				
	Loot ();																	
}																				

function Loot ()
{
	if (!clicked) 
	{																//	If not yet clicked, check Loot Table
		clicked = true;
	}
	
	//Add the item to the inventory
	Inventory.AddItem(theInventoryItem);
	
	//Destroy the game object
	Destroy(this.gameObject);
}

The problem is the fact that when I want to add theInventoryItem to my Inventory I get a error:

NullReferenceException: Object reference not set to an instance of an object
UnityScript.Lang.Extensions.get_length (System.Array a)
Inventory1.AddItem (.InventoryItem item) (at Assets/Scripts/Inventory/Inventory1.js:120)
LootObject.Loot () (at Assets/Scripts/Inventory/LootObject.js:60)
LootObject.OnMouseDown () (at Assets/Scripts/Inventory/LootObject.js:49)
UnityEngine.SendMouseEvents:smile:oSendMouseEvents()

Which I found weird because I declared the Inventory in the awake function…
So what is wrong?

Maybe this is just me being stupid but why are you declaring a new inventory in a script that goes onto a lootable object? Shouldn’t you be accessing an existing inventory that is a part of the player? The one that is declared in the inventory script here…

static var statInventory : Inventory1

yep either you have a reference to the inventory script or you have to add the class inventory inside lootable script.