Question by
jriddensdale · Nov 26, 2015 at 09:47 PM ·
nullreferenceexceptioninventorylooting
Inventory problem
So I keep on getting a null refrece exeption for my inventory!!! The line is at LootObject.js
// 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 : Inventory;
function Start ()
{ // Setup the initial states of the variables
//itemName = the object name
itemName = this.gameObject.name;
//itemIcon will get selected in Unity
//itemDescription as well
var theInventoryItem : InventoryItem = 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);
theInventoryItem = null; //Set it to null
//Destroy the game object
Destroy(this.gameObject);
}
And my Inventory script:
// 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 : Inventory; // 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, Screen.width - 10, Screen.height - 10);
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
var currentRect : Rect;
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;
}
}
Can someone please figure this out? Thanks.
Comment
Your answer
Follow this Question
Related Questions
chests and looting with ui 1 Answer
Im getting NullReferenceException error 1 Answer
How to make an animated object inside inventory. 0 Answers
Why is drag and drop not working? 1 Answer