# Problem with NullRefrence

**URL:** https://discussions.unity.com/t/problem-with-nullrefrence/1717115
**Category:** Scripting
**Tags:** Scripting, Question, 6.3
**Created:** [April 18, 2026, 6:25pm UTC](https://discussions.unity.com/t/problem-with-nullrefrence/1717115 "2026-04-18T18:25:21Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![jbpsnhr](https://avatars.discourse-cdn.com/v4/letter/j/e0b2c6/32.png) [@jbpsnhr](https://discussions.unity.com/u/jbpsnhr)
#### Post date: [April 18, 2026, 6:25pm UTC](https://discussions.unity.com/t/problem-with-nullrefrence/1717115/1 "2026-04-18T18:25:21Z")

</div>

I’m constantly getting same error  
NullReferenceException: Object reference not set to an instance of an object  
ItemWorld.SpawnItemWorld (UnityEngine.Vector3 position, Item item) (at Assets/Scriptes/Items/ItemWorld.cs:10)  
Inventory.Start () (at Assets/Scriptes/Player/UI/Inventory.cs:15)

Scripts connected to it are these:

```CSharp
using NUnit.Framework;
using UnityEngine;
using System.Collections.Generic;
using System;
public class Inventory : MonoBehaviour
{
    [SerializeField] private List<Item> itemList = new List<Item>();

    private void Start()
    {
        // Ensure inventory exists before spawning world items
        inventory();

        ItemWorld.SpawnItemWorld(new Vector2(20, 20), new Item { itemType = Item.ItemType.battery, amount = 1 });
        ItemWorld.SpawnItemWorld(new Vector2(-20, 20), new Item { itemType = Item.ItemType.bullets, amount = 1 });
        ItemWorld.SpawnItemWorld(new Vector2(20, -20), new Item { itemType = Item.ItemType.bandages, amount = 1 });
    }

    public void inventory()
    {
        itemList = new List<Item>();
        
        AddItem(new Item { itemType = Item.ItemType.battery, amount = 1 });
        AddItem(new Item { itemType = Item.ItemType.bullets, amount = 1 });
        AddItem(new Item { itemType = Item.ItemType.bandages, amount = 1 });
    }

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

    public List<Item> GetItems()
    {
        return itemList;
    }
}

```

and

```CSharp
using UnityEngine;

public class ItemWorld : MonoBehaviour
{
    public static ItemWorld Instance { get; private set; }
    public static ItemWorld SpawnItemWorld(Vector3 position, Item item)
    {
        Transform transform = Instantiate(ItemData.Instance.itemPrefab, position, Quaternion.identity);
        ItemWorld itemWorld = transform.GetComponent<ItemWorld>();
        itemWorld.SetItem(item);
        return itemWorld;
    }
    private Item item;

    private SpriteRenderer spriteRenderer;

    private void Start()
    {
        Instance = this ;
        spriteRenderer = GetComponent<SpriteRenderer>();
    }
    public void SetItem(Item item)
    {
        this.item = item;
        spriteRenderer.sprite = item.GetSprite();
    }
}

```

I tried everything I could. I don’t know what to do.

---

<div class="post-metadata">

### Author: ![jbpsnhr](https://avatars.discourse-cdn.com/v4/letter/j/e0b2c6/32.png) [@jbpsnhr](https://discussions.unity.com/u/jbpsnhr)
#### Post date: [April 18, 2026, 6:28pm UTC](https://discussions.unity.com/t/problem-with-nullrefrence/1717115/2 "2026-04-18T18:28:56Z")

</div>

Sorry but I can’t get scripts to dispay correctly

---

<div class="post-metadata">

### Author: ![MelvMay](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/melvmay/32/548775_2.png) [@MelvMay](https://discussions.unity.com/u/MelvMay)
#### Post date: [April 18, 2026, 6:58pm UTC](https://discussions.unity.com/t/problem-with-nullrefrence/1717115/3 "2026-04-18T18:58:42Z")

</div>

For scripting problems, please use the `Scripting` tag and not unrelated tags such as `2D-Physics` (I’m the 2D physics dev at Unity). Also, please use code-tags when posting code.

This way your post gets put into the correct area.

I’ll update the tags for you.

Thanks.

---

<div class="post-metadata">

### Author: ![PWSDavid](https://avatars.discourse-cdn.com/v4/letter/p/9e8a1a/32.png) [@PWSDavid](https://discussions.unity.com/u/PWSDavid)
#### Post date: [April 18, 2026, 7:15pm UTC](https://discussions.unity.com/t/problem-with-nullrefrence/1717115/5 "2026-04-18T19:15:47Z")

</div>

NullReferenceException means you are trying to access an object (for example calling a method or reading a variable on it), but that object is null.

The error you are getting tells you the file and line number the error occured on.

> ItemWorld.SpawnItemWorld (UnityEngine.Vector3 position, Item item) (at Assets/Scriptes/Items/ItemWorld.cs:10)

means the error happened on line 10 in ItemWorld.cs. You can also double click the error in the Unity console, which will open your code editor to where the error happened.

If I counted correctly, this is line 10 in ItemWorld.cs:

`itemWorld.SetItem(item);`

You are accessing itemWorld here. This line of code throwing a NullReferenceException means that itemWorld is null.

itemWorld comes from the line above that:

`ItemWorld itemWorld = transform.GetComponent();`

To the best of my knowledge, this isn’t valid code using Unitys API (GetComponent would need to get a type passed as method parameter or as generic parameter). Is it possible you made a mistake pasting your code here? Or did you really add an extension method to the Transform type?

Either way: it seems itemWorld is null, and it probably is null because there is no component called ItemWorld on the transform you are getting it from. You are getting it from the GameObject you instantiated from the prefab in ItemData.Instance.

Check the prefab that is assigned to ItemData.Instance.itemPrefab for whether it has the component ItemWorld.

Should I have miscounted or the line numbers are different in your actual code vs. what is pasted here, the answer might change slightly, but the approach to how to fix this issue remains the same. Check the line, look at what objects you are accessing, find out how any of those objects could be null.

Edit: Since your Instantiate call seems to return a _Transform_ instead of a _GameObject_ (since you are storing the result in a variable of type Transform), I’m guessing ItemData.Instance.itemPrefab is not actually pointing at a Prefab asset? I don’t think you can assign Prefabs themselves to a Transform variable. Maybe that is part of what is wrong.

---

<div class="post-metadata">

### Author: ![Kurt-Dekker](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/kurt-dekker/32/101578_2.png) [@Kurt-Dekker](https://discussions.unity.com/u/Kurt-Dekker)
#### Post date: [April 19, 2026, 1:51pm UTC](https://discussions.unity.com/t/problem-with-nullrefrence/1717115/6 "2026-04-19T13:51:12Z")

</div>

> [@jbpsnhr](#):
>
> I’m constantly getting same error

**The answer is always the same… ALWAYS!**

How to fix a NullReferenceException error

Three steps to success:

- Identify what is null ← any other action taken before this step is WASTED TIME
- Identify why it is null
- Fix that

**NullReference is the single most common error while programming. Fixing it is always the same.**

Some notes on how to fix a NullReferenceException error in Unity3D:

> **[How to find a nullref in Unity3D scripting – PLBM Games](http://plbm.com/?p=221)**

Just to keep your eyes wide open before you go too much further here:

These things (inventory, shop systems, character customization, dialog tree systems, crafting, ability unlock systems, tech trees, etc) are fairly tricky hairy beasts, definitely deep in advanced coding territory.

The following applies to ALL types of code listed above, but for simplicity I will call it “inventory.”

Inventory code never lives “all by itself.” All inventory code is EXTREMELY tightly bound to prefabs and/or assets used to display and present and control the inventory. Problems and solutions must consider both code and assets as well as scene / prefab setup and connectivity.

If you contemplate late-delivery of content (product expansion packs, DLC, etc.), all of that has to be folded into the data source architecture from the beginning.

Inventories / shop systems / character selectors all contain elements of:

- a database of items that you may possibly possess / equip
- a database of the items that you actually possess / equip currently
- perhaps another database of your “storage” area at home base?
- persistence of this information to storage between game runs
- presentation of the inventory to the user (may have to scale and grow, overlay parts, clothing, etc)
- interaction with items in the inventory or on the character or in the home base storage area
- interaction with the world to get items in and out
- dependence on asset definition (images, etc.) for presentation  
 → what it looks like lying around in the world? In a chest? On a shelf?  
 → what it looks like in the inventory window itself?  
 → what it looks like when worn by the player? Does it affect vision (binoculars, etc.)  
 → what it looks like when used, destroyed, consumed?

Just the design choices of such a system can have a lot of complicating confounding issues, such as:

- can you have multiple items? Is there a limit?
- if there is an item limit, what is it? Total count? Weight? Size? Something else?
- are those items shown individually or do they stack?
- are coins / gems stacked but other stuff isn’t stacked?
- do items have detailed data shown (durability, rarity, damage, etc.)?
- can users combine items to make new items? How? Limits? Results? Messages of success/failure?
- can users substantially modify items with other things like spells, gems, sockets, etc.?
- does a worn-out item (shovel) become something else (like a stick) when the item wears out fully?
- etc.

Your best bet is probably to write down exactly what you want feature-wise. It may be useful to get very familiar with an existing game so you have an actual example of each feature in action.

Once you have decided a baseline design, fully work through two or three different inventory tutorials on Youtube, perhaps even for the game example you have chosen above.

Breaking down a large problem such as inventory:

> [@Weapon Inventory and how to script weapons.](https://discussions.unity.com/t/weapon-inventory-and-how-to-script-weapons/826141/4):
>
> You’re welcome of course. One thing you could try to really get a “feel” for what happens in this case is make a simple script that is just one cell of your inventory. Put this into a script that you can access through an “inventory API” that does certain things to a cell. These functions might be: “Set the type of item” “Query the type and quantity of item in this cell” “Add 1 item to this cell.” “Add N number of items to this cell.” “Remove N item from this cell.” Then you can have pr…

If you want to see most of the steps involved, make a “micro inventory” in your game, something whereby the player can have (or not have) a single item, and display that item in the UI, and let the user select that item and do things with it (take, drop, use, wear, eat, sell, buy, etc.).

Everything you learn doing that “micro inventory” of one item will apply when you have any larger more complex inventory, and it will give you a feel for what you are dealing with.

Breaking down large problems in general:

> [@Options for implementing mining/digging in an asteroid belt game?](https://discussions.unity.com/t/options-for-implementing-mining-digging-in-an-asteroid-belt-game/908126/3):
>
> Whenever engineering seems too much to tackle, it’s always best to see if you can break the problem down. What you describe above is by no means simple, as just the different ways all of it could be implemented are complex: does the camera rotate with the player on the asteroid? Do the player’s controls rotate as they walk around the asteroid? Does the camera lock to the rotating asteroid? What does the camera do if the player digs down a ways? If the player digs a tunnel into the asteroid, d…

The moment you put an inventory system into place is also a fantastic time to consider your data lifetime and persistence. Create a load/save game and put the inventory data store into that load/save data area and begin loading/saving the game state every time you run / stop the game. Doing this early in the development cycle will make things much easier later on.

Various possible inventory data structures in Unity3D:

> [@Inventory/Item system](https://discussions.unity.com/t/inventory-item-system/921694):
>
> So i’ve been trying for a while now to figure out a good item system for my game. I thought I found a solution by using GameObjects as items, since it checked all of my requirements: The item needs to be visible when equipped (I just position the GameObject to the correct slot) The item needs to be an instance, that is, have unique values such as durability and other fields specific to certain items It needs to have a pickup with proper colliders. That is, the colliders should match the shape …

---

<div class="post-metadata">

### Author: ![jbpsnhr](https://avatars.discourse-cdn.com/v4/letter/j/e0b2c6/32.png) [@jbpsnhr](https://discussions.unity.com/u/jbpsnhr)
#### Post date: [April 19, 2026, 6:53pm UTC](https://discussions.unity.com/t/problem-with-nullrefrence/1717115/7 "2026-04-19T18:53:42Z")

</div>

It has been solved Item was not beging refrenced properly
