Search Unity

NullReferenceException: Object reference not set to an instance of an object

Discussion in 'Scripting' started by Control-definition-launch-file, Jun 14, 2019.

  1. Control-definition-launch-file

    Control-definition-launch-file

    Joined:
    Jun 12, 2019
    Posts:
    7
    So I keep getting this error with my code for an inventory system. I see nothing wrong in the code, so there must be a different way to write it. Could someone tell me what is missing in this?

    Inventory Script
    Code (CSharp):
    1. public class Inventory : MonoBehaviour
    2. {
    3.     public GameObject player;
    4.     public GameObject Bar;
    5.  
    6.     public static Inventory instance;
    7.  
    8.     public List<Item> list = new List<Item>();
    9.  
    10.     public void UpdatePanelSlots()
    11.     {
    12.         int index = 0;
    13.         foreach (Transform child in Bar.transform)
    14.  
    15.         {
    16.             InventorySlotController slot = child.GetComponent<InventorySlotController>();
    17.  
    18.             if (index < list.Count)
    19.             {
    20.                 slot.item = list[index];
    21.             }
    22.             else
    23.             {
    24.                 slot.item = null;
    25.             }
    26.             slot.Updateinfo();
    27.             index++;
    28.         }
    29.     }
    30.     void Start()
    31.     {
    32.         instance = this;
    33.         UpdatePanelSlots();
    34.     }
    35.     public void Add(Item item)
    36.     {
    37.         if (list.Count < 3)
    38.         {
    39.             list.Add(item);
    40.         }
    41.         UpdatePanelSlots();
    42.     }
    43.     public void Remove(Item item)
    44.     {
    45.         list.Remove(item);
    46.         UpdatePanelSlots();
    47.     }
    48. }
    49.  
    50.  
    InventorySlotController Script
    Code (CSharp):
    1. public class InventorySlotController : MonoBehaviour
    2. {
    3.     public Item item;
    4.  
    5.     private void Start()
    6.     {
    7.         Updateinfo();
    8.     }
    9.  
    10.     public void Use()
    11.     {
    12.         if (item)
    13.         {
    14.             item.Use();
    15.         }
    16.     }
    17.     public void Updateinfo()
    18.     {
    19.         Image displayImage = transform.Find("Image").GetComponent<Image>();
    20.  
    21.         if (item)
    22.         {
    23.             displayImage.sprite = item.icon;
    24.         }
    25.         else
    26.         {
    27.             displayImage.sprite = null;
    28.         }
    29.     }
    30. }
    31.  
    Item Script
    Code (CSharp):
    1. public class Item : ScriptableObject
    2. {
    3.     public string itemName;
    4.     public Sprite icon;
    5.  
    6.     public virtual void Use()
    7.     {
    8.  
    9.     }
    10. }
     
  2. WallaceT_MFM

    WallaceT_MFM

    Joined:
    Sep 25, 2017
    Posts:
    394
    Please copy and paste the error from the console. It contains information about what line is throwing the error.
     
  3. Control-definition-launch-file

    Control-definition-launch-file

    Joined:
    Jun 12, 2019
    Posts:
    7
    NullReferenceException: Object reference not set to an instance of an object
    Inventory.UpdatePanelSlots () (at Assets/Scripts/Inventory.cs:28)
    Inventory.Start () (at Assets/Scripts/Inventory.cs:37)
     
  4. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    This error often comes not from code, but from data. Look at the line what throws the error and check object required exists and fields in inspector are set to valid references. Basically this error means you're trying to access something what do not exists.
     
  5. EdGunther

    EdGunther

    Joined:
    Jun 25, 2018
    Posts:
    183
    check the inspector and make sure the public variables are referencing an object
     
  6. EdGunther

    EdGunther

    Joined:
    Jun 25, 2018
    Posts:
    183
    double click the error on console and it should highlight the line throwing the error in visual studio
     
  7. WallaceT_MFM

    WallaceT_MFM

    Joined:
    Sep 25, 2017
    Posts:
    394
    I'm guessing that your "Bar" game object has some child objects that don't have an InventorySlotController, maybe some graphics-only objects?
     
    palex-nx likes this.