# NullReferenceException (782900)

**URL:** https://discussions.unity.com/t/nullreferenceexception-782900/782900
**Category:** Scripting
**Tags:** Scripting
**Created:** [March 29, 2020, 6:26am UTC](https://discussions.unity.com/t/nullreferenceexception-782900/782900 "2020-03-29T06:26:10Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![ganesh\_be](https://avatars.discourse-cdn.com/v4/letter/g/858c86/32.png) [@ganesh\_be](https://discussions.unity.com/u/ganesh_be)
#### Post date: [March 29, 2020, 6:26am UTC](https://discussions.unity.com/t/nullreferenceexception-782900/782900/1 "2020-03-29T06:26:10Z")

</div>

I am working on a 3D game and in the scripting part, I am having a NullReference Exception.

Someone please please help me, stuck for a long time.

---

<div class="post-metadata">

### Author: ![ganesh\_be](https://avatars.discourse-cdn.com/v4/letter/g/858c86/32.png) [@ganesh\_be](https://discussions.unity.com/u/ganesh_be)
#### Post date: [March 29, 2020, 7:10am UTC](https://discussions.unity.com/t/nullreferenceexception-782900/782900/2 "2020-03-29T07:10:58Z")

</div>

NullReferenceException: Object reference not set to an instance of an object  
Photo.OnDrop () (at Assets/Scripts/Photo.cs:44)  
Inventory.RemoveItem (IInventoryItem item) (at Assets/Scripts/Inventory.cs:44)  
ItemDropHandler.OnDrop (UnityEngine.EventSystems.PointerEventData eventData) (at Assets/Scripts/ItemDropHandler.cs:27)  
UnityEngine.EventSystems.ExecuteEvents.Execute (UnityEngine.EventSystems.IDropHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:85)  
UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1] functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:261)  
UnityEngine.EventSystems.EventSystem:Update()

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

public class Photo : InventoryItemBase
{
    public GameObject replace;

    public Inventory inventory;

    public HUD Hud;
    // Start is called before the first frame update
    public override string Name
    {
        get
        {
            return "Photo";
        }
    }

    public override void OnDrop()
    {
        RaycastHit hit = new RaycastHit();
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        replace = GameObject.FindGameObjectWithTag("replace");
        if (Physics.Raycast(ray, out hit, 1000))
        {
            //TODO item drop functionality on a fixed wall
            gameObject.SetActive(true);
            //gameObject.transform.position = hit.point;
            //Destroy(replace.gameObject);
            if (hit.collider.gameObject.CompareTag("replace"))
            {
               // gameObject.SetActive(true);
                gameObject.transform.position = replace.gameObject.transform.position;
                gameObject.transform.rotation = replace.gameObject.transform.rotation;
                Destroy(replace.gameObject);
              
            }
            else
            {
                gameObject.SetActive(false);
                IInventoryItem _item = gameObject.GetComponent<IInventoryItem>();
                inventory.AddItem(_item);
                _item.OnPickup();
                Hud.CloseMessagePanel();
            }
         
        }
    }
}

```

---

<div class="post-metadata">

### Author: ![davidnibi](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/davidnibi/32/243542_2.png) [@davidnibi](https://discussions.unity.com/u/davidnibi)
#### Post date: [March 29, 2020, 8:28am UTC](https://discussions.unity.com/t/nullreferenceexception-782900/782900/3 "2020-03-29T08:28:05Z")

</div>

You should put the code up you’re having trouble with 🙂

---

<div class="post-metadata">

### Author: ![ganesh\_be](https://avatars.discourse-cdn.com/v4/letter/g/858c86/32.png) [@ganesh\_be](https://discussions.unity.com/u/ganesh_be)
#### Post date: [March 29, 2020, 10:08am UTC](https://discussions.unity.com/t/nullreferenceexception-782900/782900/4 "2020-03-29T10:08:30Z")

</div>

> [@davidnibi](#):
>
> You should put the code up you’re having trouble with 🙂

I am having trouble with this code at line number 44

---

<div class="post-metadata">

### Author: ![davidnibi](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/davidnibi/32/243542_2.png) [@davidnibi](https://discussions.unity.com/u/davidnibi)
#### Post date: [March 29, 2020, 12:23pm UTC](https://discussions.unity.com/t/nullreferenceexception-782900/782900/5 "2020-03-29T12:23:24Z")

</div>

```csharp
inventory.AddItem(_item);

```

I’m guessing \_item doesn’t exist when you’re trying to add it because:

```csharp
IInventoryItem _item = gameObject.GetComponent<IInventoryItem>();

```

can’t GetComponent because it’s not there. Null Reference always means GetComponent can’t find the thing it’s meant to be getting and returning this error. IInventoryItem isn’t declred in this class so I don’t know what that’s doing.  
If you make and IInventoryItem public at the start of your code, you will be able to see in the inspector which is the problem.
