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

Hi all I’m pretty new to unity and c# coding. I’m trying to make a simple inventory system following along with a tutorial, but I’ve run into an issue that I can’t figure out. The error log is returning a NullReferenceException: Object reference not set to an instance of an object error and I’ve triple checked my code and it’s exactly the same as the tutorial so I’m at a bit of a loss. Here’s what I have that I think pertains to the issue:

PlayerController.cs

[SerializeField] private UI_Inventory uiInventory;
private Inventory inventory;

private void Awake()
{
anim = GetComponent();
rb = GetComponent();
jump = new Vector3(0.0f, 1.0f, 0.0f);

inventory = new Inventory();
uiInventory.SetInventory(inventory);
}

UI_Inventory.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class UI_Inventory : MonoBehaviour
{
private Inventory inventory;
private Transform itemSlotContainer;
private Transform itemSlotTemplate;

private void Awake(){
itemSlotContainer = transform.Find(“itemSlotContainer”);
itemSlotTemplate = itemSlotContainer.Find(“itemSlotTemplate”);
}
public void SetInventory(Inventory inventory){
this.inventory = inventory;
RefreshInventoryItems();

}

private void RefreshInventoryItems(){
int x = 0;
int y = 0;
float itemSlotCellSize = 30f;
foreach (Item item in inventory.GetItemList()){
RectTransform itemSlotRectTransform = Instantiate(itemSlotTemplate, itemSlotContainer).GetComponent();
itemSlotRectTransform.gameObject.SetActive(true);

itemSlotRectTransform.anchoredPosition = new Vector2(x * itemSlotCellSize, -y * itemSlotCellSize);
Image image = itemSlotRectTransform.Find(“image”).GetComponent();
image.sprite = item.GetSprite();

x++;
if (x > 4){
x = 0;
y++;
}
}
}
}

Inventory.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Inventory
{
private List itemList;

public Inventory() {
itemList = new List();
AddItem(new Item { itemType = Item.ItemType.Flower, amount = 1});
AddItem(new Item { itemType = Item.ItemType.Bug, amount = 1});
AddItem(new Item { itemType = Item.ItemType.Water, amount = 1});
AddItem(new Item { itemType = Item.ItemType.Jar, amount = 1});

}

public void AddItem(Item item){
itemList.Add(item);

}

public List GetItemList(){
return itemList;
}

}

Please help! I’ll upload my files as well for the whole thing

7705684–965488–Inventory.cs (679 Bytes)
7705684–965491–Item.cs (591 Bytes)
7705684–965494–ItemAssets.cs (379 Bytes)
7705684–965497–PlayerController.cs (4.08 KB)
7705684–965500–UI_Inventory.cs (1.29 KB)

Not necessary at all… there’s nothing we can do here to find your NullReference.

Here is how YOU can find it however!

The answer is always the same… ALWAYS. It is the single most common error ever.

Don’t waste your life spinning around and round on this error. Instead, learn how to fix it fast… it’s EASY!!

Some notes on how to fix a NullReferenceException error in Unity3D

  • also known as: Unassigned Reference Exception
  • also known as: Missing Reference Exception
  • also known as: Object reference not set to an instance of an object

http://plbm.com/?p=221

The basic steps outlined above are:

  • Identify what is null
  • Identify why it is null
  • Fix that.

Expect to see this error a LOT. It’s easily the most common thing to do when working. Learn how to fix it rapidly. It’s easy. See the above link for more tips.

You need to figure out HOW that variable is supposed to get its initial value. There are many ways in Unity. In order of likelihood, it might be ONE of the following:

  • drag it in using the inspector
  • code inside this script initializes it
  • some OTHER external code initializes it
  • ? something else?

This is the kind of mindset and thinking process you need to bring to this problem:

https://discussions.unity.com/t/814091/4

Step by step, break it down, find the problem.

Here is a clean analogy of the actual underlying problem of a null reference exception:

https://discussions.unity.com/t/840647/7

1 Like

Any tips on how to know what variable is null?

The first small link contains the tips. If you don’t understand the tips then you need to say which parts you don’t understand because it’s pretty straightforward stuff.

I got this a lot when I was starting monodevelop/C#, as said, it’s one of the most common errors you will see.
It’s always for a variable etc you are trying to access that you haven’t set yet.
Most common is like “GetComponent” on a variable that is null/blank (I found).

Setting every variable to public and checking if each one is occupied or empty is a good way of narrowing it down.

this is my problem Can some help me I’m a beginner in unity

Please do not necro-post for something as utterly trivial as a null reference exception. You can fix that yourself. Here is how:

How to fix a NullReferenceException error

https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/

Three steps to success:

  • Identify what is null
  • Identify why it is null
  • Fix that