# NullReferenceException Error (932136)

**URL:** https://discussions.unity.com/t/nullreferenceexception-error-932136/932136
**Category:** Scripting
**Tags:** Scripting, Question
**Created:** [October 27, 2023, 4:58pm UTC](https://discussions.unity.com/t/nullreferenceexception-error-932136/932136 "2023-10-27T16:58:35Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Starfire2512](https://avatars.discourse-cdn.com/v4/letter/s/848f3c/32.png) [@Starfire2512](https://discussions.unity.com/u/Starfire2512)
#### Post date: [October 27, 2023, 4:58pm UTC](https://discussions.unity.com/t/nullreferenceexception-error-932136/932136/1 "2023-10-27T16:58:35Z")

</div>

Hello I have this error, what did I do wrong?

> [@](#):
>
> NullReferenceException: Object reference not set to an instance of an object  
> ItemDragAndDropController.Update () (at Assets/ItemDragAndDropController.cs:35)

```csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;

public class ItemDragAndDropController : MonoBehaviour
{

    [SerializeField] ItemSlot itemSlot;
    [SerializeField] GameObject itemIcon;
    RectTransform iconTransform;
    Image itemIconImage;

    private void Start()
    {
        itemSlot = new ItemSlot();
        iconTransform = itemIcon.GetComponent<RectTransform>();
        itemIconImage = itemIcon.GetComponent<Image>();
    }

    private void Update()
    {
        if (itemIcon.activeInHierarchy == true)
        {
            iconTransform.position = Input.mousePosition;

            if (Input.GetMouseButtonDown(0))
            {
                if (EventSystem.current.IsPointerOverGameObject() == false)
                {
                    Vector3 worldPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                    worldPosition.z = 0;

                    ItemSpawnManager.instance.SpawnItem(
                        worldPosition,
                        itemSlot.item,
                        itemSlot.count
                        );
                }
            }
        }
    }

    internal void OnClick(ItemSlot itemSlot)
    {
        if (this.itemSlot.item == null)
        {
            this.itemSlot.Copy(itemSlot);
            itemSlot.Clear();
        }
        else{
            Item item = itemSlot.item;
            int count = itemSlot.count;

            itemSlot.Copy(this.itemSlot);
            this.itemSlot.Set(item, count);
        }
        UpdateIcon();
    }

    private void UpdateIcon()
    {
        if (itemSlot.item == null)
        {
            itemIcon.SetActive(false);
        }
        else {
            itemIcon.SetActive(true);
            itemIconImage.sprite = itemSlot.item.icon;
        }
    }

}

```

---

<div class="post-metadata">

### Author: ![AngryProgrammer](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/angryprogrammer/32/319731_2.png) [@AngryProgrammer](https://discussions.unity.com/u/AngryProgrammer)
#### Post date: [October 27, 2023, 5:06pm UTC](https://discussions.unity.com/t/nullreferenceexception-error-932136/932136/2 "2023-10-27T17:06:47Z")

</div>

ItemSpawnManager.instance don’t exist aka is null.

---

<div class="post-metadata">

### Author: ![Starfire2512](https://avatars.discourse-cdn.com/v4/letter/s/848f3c/32.png) [@Starfire2512](https://discussions.unity.com/u/Starfire2512)
#### Post date: [October 27, 2023, 5:09pm UTC](https://discussions.unity.com/t/nullreferenceexception-error-932136/932136/3 "2023-10-27T17:09:09Z")

</div>

I have another file here

```csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ItemSpawnManager : MonoBehaviour
{
    public static ItemSpawnManager instance;

    private void Awake()
    {
        instance = this;
    }

    [SerializeField] GameObject pickUpItemPrefab;

    public void SpawnItem(Vector3 position, Item item, int count)
    {
        GameObject o = Instantiate(pickUpItemPrefab, position, Quaternion.identity);
        o.GetComponent<PickUpItem>().Set(item, count);
    }
}

```

---

<div class="post-metadata">

### Author: ![AngryProgrammer](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/angryprogrammer/32/319731_2.png) [@AngryProgrammer](https://discussions.unity.com/u/AngryProgrammer)
#### Post date: [October 27, 2023, 5:11pm UTC](https://discussions.unity.com/t/nullreferenceexception-error-932136/932136/4 "2023-10-27T17:11:02Z")

</div>

About above script you drag and drop it to some object in hierarchy? So it can use Awake method.

---

<div class="post-metadata">

### Author: ![Starfire2512](https://avatars.discourse-cdn.com/v4/letter/s/848f3c/32.png) [@Starfire2512](https://discussions.unity.com/u/Starfire2512)
#### Post date: [October 27, 2023, 5:15pm UTC](https://discussions.unity.com/t/nullreferenceexception-error-932136/932136/5 "2023-10-27T17:15:36Z")

</div>

So at the moment I only get this NullReferenceException error when I throw something on the ground from my inventory in the game, I don’t get any other errors.

---

<div class="post-metadata">

### Author: ![AngryProgrammer](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/angryprogrammer/32/319731_2.png) [@AngryProgrammer](https://discussions.unity.com/u/AngryProgrammer)
#### Post date: [October 27, 2023, 5:17pm UTC](https://discussions.unity.com/t/nullreferenceexception-error-932136/932136/6 "2023-10-27T17:17:11Z")

</div>

I asked if you drag and drop ItemSpawnManager on any game object in hierarchy. If not its instance will be always null and result will be error like you posted.

---

<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: [October 27, 2023, 5:25pm UTC](https://discussions.unity.com/t/nullreferenceexception-error-932136/932136/7 "2023-10-27T17:25:24Z")

</div>

> [@Starfire2512](#):
>
> I have this error,

The answer is always the same… ALWAYS!

How to fix a NullReferenceException error

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

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

---

<div class="post-metadata">

### Author: ![Starfire2512](https://avatars.discourse-cdn.com/v4/letter/s/848f3c/32.png) [@Starfire2512](https://discussions.unity.com/u/Starfire2512)
#### Post date: [October 28, 2023, 12:58pm UTC](https://discussions.unity.com/t/nullreferenceexception-error-932136/932136/8 "2023-10-28T12:58:16Z")

</div>

I fixed the problem, thank you.
