I’m somewhat new to Unity and game development but I keep getting this error:
NullReferenceException: Object reference not set to an instance of an object
HUD.InventoryScript_ItemAdded (System.Object sender, .InventoryEventArgs e) (at Assets/HUD.cs:21)
inventory.AddItem (IInventoryItem item) (at Assets/Scripts/inventory.cs:42)
collider.OnCollisionEnter (UnityEngine.Collision col) (at Assets/Scripts/collider.cs:38)
Usually when I get a NullReferenceException, the fixes is dragging in the object that a script is referring to. From what I can see here, the collider (which is on the player) is detecting the collision between the player and the item. It sent the information of collision object to the inventory class which adds the object to the List of inventory items. From here things get a bit murky as it throws the error in the HUD.InventoryScript_ItemAdded method.
Here is the HUD.cs script
using UnityEngine;
using UnityEngine.UI;
public class HUD : MonoBehaviour {
public inventory Inventory;
// Use this for initialization
void Start () {
Inventory.ItemAdded += InventoryScript_ItemAdded;
}
private void InventoryScript_ItemAdded(object sender, InventoryEventArgs e)
{
Transform inventoryPanel = transform.Find("InventoryPanel");
foreach(Transform slot in inventoryPanel)
{
//Border to image
Image image = slot.GetChild(0).GetChild(0).GetComponent<Image>();
//We found the empty slot
if (!image.enabled)
{
image.enabled = true;
image.sprite = e.Item.Image;
//TODO: Store reference to the item
break; //what does this do?
}
}
}
// Update is called once per frame
void Update () {
}
}
This is the inventory script
public class inventory : MonoBehaviour {
//fixed number of slots
private const int SLOTS = 10;
//creates an arraylist of inventory items
private List<IInventoryItem> mItems = new List<IInventoryItem>();
//handles event?
public event EventHandler<InventoryEventArgs> ItemAdded;
//adding item method
public void AddItem(IInventoryItem item)
{
//if there are free slots available
if (mItems.Count < SLOTS)
{
//collide with item
Collider collider = (item as MonoBehaviour).GetComponent<Collider>();
if (collider.enabled)
{
//if the collider of the object is enabled turn it off
collider.enabled = false;
//add to array list
mItems.Add(item);
item.OnPickup();
//all subscribers of event are notfified?
if(ItemAdded != null)
{
ItemAdded(this,new InventoryEventArgs(item));
}
}
}
}
}
And finally this is the collider script I put on my player
public class collider : MonoBehaviour
{
public inventory Inventory;
//public GameObject pickupEffect;
public gun g;
void OnTriggerEnter(Collider other)
{
if(other.gameObject.tag == "item")
{
Debug.Log("You Hit an item");
Destroy(other.gameObject);
}
if(other.gameObject.tag == "ammo")
{
Debug.Log("Picked up ammo");
Destroy(other.gameObject);
g.ammo += 30;
}
}
void OnCollisionEnter(Collision col)
{
if(col.gameObject.tag == "collectable")
{
Debug.Log("This object is collectable");
IInventoryItem item = col.gameObject.GetComponent<IInventoryItem>();
if (item != null)
{
Inventory.AddItem(item);
Debug.Log("Collided with an inventory item");
}
}
}
/*
*
//check if item collided with has an inventory item interface
private void OnControllerColliderHit(ControllerColliderHit hit)
{
Debug.Log("Collided with an inventory item");
IInventoryItem item = hit.collider.GetComponent<IInventoryItem>();
if(item != null)
{
Inventory.AddItem(item);
Debug.Log("Collided with an inventory item");
}
}
*/
}
Sorry if the code is not formatted correctly but the system for creating code in this forum seems to be a bit tedious because you have to space 4 times for each line.
Anyway, why am I getting this error and how could I fix it while implementing this code?