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 _= items*;*_
* }*
* for (int i = 0; i < 4; i++)*
* {*
armor = items*;*
* }*
* }*
* 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().UpdateHotbar(hotbar);*
* FindObjectOfType().UpdateMainItem(mainItem);*
* }*
* private void prevItem()*
* {*
* Sprite temp = mainItem;*
* mainItem = hotbar[2];*
* hotbar[2] = hotbar[1];*
* hotbar[1] = hotbar[0];*
* hotbar[0] = temp;*
* FindObjectOfType().UpdateHotbar(hotbar);*
* FindObjectOfType().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().UpdateInventory();*
* GameObject.Find(“InventoryManager”).GetComponent().UpdateHotbar();*
* GameObject.Find(“InventoryManager”).GetComponent().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().hotbar;*
* for(int i = 0; i < hotbar.Length; i++)*
{
hotbar_.sprite = toLoad*;
}
}*_
* //setting the armor slots to the correct values*
* public void UpdateArmor()*
* {*
* Sprite[] toLoad = GameObject.Find(“Inventory”).GetComponent().armor;*
* for(int i = 0;i < armor.Length; i++)*
{
_ armor*.sprite = toLoad[0];
}
}*_
* //setting the inventory slots to the correct values*
* public void UpdateInventory()*
* {*
* Sprite[] toLoad = GameObject.Find(“Inventory”).GetComponent().inventory;*
* for (int i = 0; i < inventory.Length; i++)*
* {*
_ inventory*.sprite = toLoad[0];
}
}
}*_
If anyone knows why I’m getting this error please let me know.
Thanks in advance.