I am working on an inventory and every time I opne it I get this error: NullReferenceException: Object reference not set to an instance of an object Inventory.LoadUnloadInventoryMenu () (at Assets/Scripts/Inventory.cs:116) Inventory.Update () (at Assets/Scripts/Inventory.cs:59)
This is the inventory.cs script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Inventory : MonoBehaviour
{
//Keys used to open doors
public bool hasRedKey, hasGreenKey, hasBlueKey;
//the item the player is using
public Sprite mainItem;
public Sprite[] hotbar = new Sprite[3];
//every item in the game
public Sprite[] items = new Sprite[30];
//items in the players inventory
public Sprite[] inventory = new Sprite[20];
//armor slots in thye players Inventory
public Sprite[] armor = new Sprite[4];
//the inventory menu
public GameObject menu;
void Start()
{
//Deactivating the menu
menu.SetActive(false);
//assinging values
mainItem = items[0];
FindObjectOfType<CanvasManager>().UpdateMainItem(mainItem);
hotbar[0] = items[3];
hotbar[1] = items[20];
hotbar[2] = items[4];
FindObjectOfType<CanvasManager>().UpdateHotbar(hotbar);
for(int i = 0; i < 20; i++)
{
inventory[i] = items[i];
}
for (int i = 0; i < 4; i++)
{
armor[i] = items[i];
}
}
void Update()
{
//checking if the player opened/ closed the inventory
if (Input.GetKeyDown(KeyCode.E))
{
LoadUnloadInventoryMenu();
}
//cycle through hotbar
if (Input.mouseScrollDelta.y == 1)
{
nextItem();
}
if (Input.mouseScrollDelta.y == -1)
{
prevItem();
}
}
private void nextItem()
{
Sprite temp = hotbar[0];
hotbar[0] = hotbar[1];
hotbar[1] = hotbar[2];
hotbar[2] = mainItem;
mainItem = temp;
FindObjectOfType<CanvasManager>().UpdateHotbar(hotbar);
FindObjectOfType<CanvasManager>().UpdateMainItem(mainItem);
}
private void prevItem()
{
Sprite temp = mainItem;
mainItem = hotbar[2];
hotbar[2] = hotbar[1];
hotbar[1] = hotbar[0];
hotbar[0] = temp;
FindObjectOfType<CanvasManager>().UpdateHotbar(hotbar);
FindObjectOfType<CanvasManager>().UpdateMainItem(mainItem);
}
private void LoadUnloadInventoryMenu()
{
if (menu.activeSelf)
{
//diactivatting the inventory when its open
menu.SetActive(false);
}
else
{
//activatting the inventory
menu.SetActive(true);
//null check
if(inventory != null && armor != null && hotbar != null)
{
//update the items to the correct value
GameObject.Find("InventoryManager").GetComponent<InventoryManager>().UpdateInventory();
GameObject.Find("InventoryManager").GetComponent<InventoryManager>().UpdateHotbar();
GameObject.Find("InventoryManager").GetComponent<InventoryManager>().UpdateArmor();
}
}
}
}
The Canvas manager is used to display the UI visible throughout the game that displays things like health to the player. The "menu" object I assigned manually in the Unity Inspector as the InventoryManager object which is the InventoryUI. This is the InventoryManager.cs file:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class InventoryManager : MonoBehaviour
{
//definning inventory slots
public Image[] inventory = new Image[20];
public Image[] armor = new Image[4];
public Image[] hotbar = new Image[3];
//setting the hotbar slots to the correct values
public void UpdateHotbar()
{
Sprite[] toLoad = GameObject.Find("Inventory").GetComponent<Inventory>().hotbar;
for(int i = 0; i < hotbar.Length; i++)
{
hotbar[i].sprite = toLoad[i];
}
}
//setting the armor slots to the correct values
public void UpdateArmor()
{
Sprite[] toLoad = GameObject.Find("Inventory").GetComponent<Inventory>().armor;
for(int i = 0;i < armor.Length; i++)
{
armor[i].sprite = toLoad[0];
}
}
//setting the inventory slots to the correct values
public void UpdateInventory()
{
Sprite[] toLoad = GameObject.Find("Inventory").GetComponent<Inventory>().inventory;
for (int i = 0; i < inventory.Length; i++)
{
inventory[i].sprite = toLoad[0];
}
}
}
If anyone know when I'm getting this error please let me know. Thanks in advance.