title: How to fix NullReferenceException: Object reference not set to an instance of an object error in Unity
body: <p>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)</p>
  <p>This is the inventory.cs script:</p>
  <pre><code>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&lt;CanvasManager&gt;().UpdateMainItem(mainItem);
          hotbar[0] = items[3];
          hotbar[1] = items[20];
          hotbar[2] = items[4];
          FindObjectOfType&lt;CanvasManager&gt;().UpdateHotbar(hotbar);
  
          for(int i = 0; i &lt; 20; i++)
          {
              inventory[i] = items[i];
          }
  
          for (int i = 0; i &lt; 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&lt;CanvasManager&gt;().UpdateHotbar(hotbar);
          FindObjectOfType&lt;CanvasManager&gt;().UpdateMainItem(mainItem);
  
      }
  
      private void prevItem()
      {
          Sprite temp = mainItem;
          mainItem = hotbar[2];
          hotbar[2] = hotbar[1];
          hotbar[1] = hotbar[0];
          hotbar[0] = temp;
          FindObjectOfType&lt;CanvasManager&gt;().UpdateHotbar(hotbar);
          FindObjectOfType&lt;CanvasManager&gt;().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 &amp;&amp; armor != null &amp;&amp; hotbar != null)
              {
  
                  //update the items to the correct value
                  GameObject.Find(&quot;InventoryManager&quot;).GetComponent&lt;InventoryManager&gt;().UpdateInventory();
                  GameObject.Find(&quot;InventoryManager&quot;).GetComponent&lt;InventoryManager&gt;().UpdateHotbar();
                  GameObject.Find(&quot;InventoryManager&quot;).GetComponent&lt;InventoryManager&gt;().UpdateArmor();
  
              }
                  
          }
              
      }
  }
  </code></pre>
  <p>The Canvas manager is used to display the UI visible throughout the game that displays things like health to the player. The &quot;menu&quot; object I assigned manually in the Unity Inspector as the InventoryManager object which is the InventoryUI.
  This is the InventoryManager.cs file:</p>
  <pre><code>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(&quot;Inventory&quot;).GetComponent&lt;Inventory&gt;().hotbar;
  
          for(int i = 0; i &lt; hotbar.Length; i++)
          {
              hotbar[i].sprite = toLoad[i];
          }
      }
  
      //setting the armor slots to the correct values
      public void UpdateArmor()
      {
          Sprite[] toLoad = GameObject.Find(&quot;Inventory&quot;).GetComponent&lt;Inventory&gt;().armor;
  
          for(int i = 0;i &lt; armor.Length; i++)
          {
              armor[i].sprite = toLoad[0];
          }
      }
  
      //setting the inventory slots to the correct values
      public void UpdateInventory()
      {
          Sprite[] toLoad = GameObject.Find(&quot;Inventory&quot;).GetComponent&lt;Inventory&gt;().inventory;
  
          for (int i = 0; i &lt; inventory.Length; i++)
          {
              inventory[i].sprite = toLoad[0];
          }
      }
  }
  
  </code></pre>
  <p>If anyone know when I'm getting this error please let me know.
  Thanks in advance.</p>