Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Need Help NullReferenceException

Discussion in 'Scripting' started by ganesh_be, Mar 29, 2020.

  1. ganesh_be

    ganesh_be

    Joined:
    Mar 10, 2020
    Posts:
    16
    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.

    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()
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Photo : InventoryItemBase
    6. {
    7.     public GameObject replace;
    8.  
    9.     public Inventory inventory;
    10.  
    11.     public HUD Hud;
    12.     // Start is called before the first frame update
    13.     public override string Name
    14.     {
    15.         get
    16.         {
    17.             return "Photo";
    18.         }
    19.     }
    20.  
    21.     public override void OnDrop()
    22.     {
    23.         RaycastHit hit = new RaycastHit();
    24.         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    25.         replace = GameObject.FindGameObjectWithTag("replace");
    26.         if (Physics.Raycast(ray, out hit, 1000))
    27.         {
    28.             //TODO item drop functionality on a fixed wall
    29.             gameObject.SetActive(true);
    30.             //gameObject.transform.position = hit.point;
    31.             //Destroy(replace.gameObject);
    32.             if (hit.collider.gameObject.CompareTag("replace"))
    33.             {
    34.                // gameObject.SetActive(true);
    35.                 gameObject.transform.position = replace.gameObject.transform.position;
    36.                 gameObject.transform.rotation = replace.gameObject.transform.rotation;
    37.                 Destroy(replace.gameObject);
    38.              
    39.             }
    40.             else
    41.             {
    42.                 gameObject.SetActive(false);
    43.                 IInventoryItem _item = gameObject.GetComponent<IInventoryItem>();
    44.                 inventory.AddItem(_item);  // This line gives me error
    45.                 _item.OnPickup();
    46.                 Hud.CloseMessagePanel();
    47.             }
    48.          
    49.         }
    50.     }
    51. }
     
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    Hey, a NullRefernceException basically means that you are trying to work on an object, but the corresponding object reference does not exist / was not set yet (or removed since) - hence is 'null'.

    In your case, the exception is caused in line 44 in the OnDrop method of the Photo class, as stated in the error message. There you write 'inventory.AddItem(_item)'. This means that 'inventory' must be null, since <null>.AddItem is the only way to cause a null reference exception in that line.

    In your script you are never setting inventory to be something (ie inventory = ...). So the only way for it to contain a reference would be to set it through the inspector, which you probably intend to do since inventory is a public variable (?). Either way, you need to fill the variable 'inventory' with an actual object. Either by assigning it through script, or if Inventory is also a Monobehaviour, by dragging the correct gameobject into the field in the inspector.

    Hope the above explanation helps you to get a grasp for how to figure out what's wrong as well :)
     
  3. ganesh_be

    ganesh_be

    Joined:
    Mar 10, 2020
    Posts:
    16
    Thank you very much. You saved my day.