Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
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
Add comment
10 |3000 characters needed characters left characters exceeded
â–¼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

34 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

GetComponent.sprite is not valide. please help 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges