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

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.
     
    Last edited: Mar 29, 2020
  2. ganesh_be

    ganesh_be

    Joined:
    Mar 10, 2020
    Posts:
    16
    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);
    45.                 _item.OnPickup();
    46.                 Hud.CloseMessagePanel();
    47.             }
    48.          
    49.         }
    50.     }
    51. }
     
  3. davidnibi

    davidnibi

    Joined:
    Dec 19, 2012
    Posts:
    424
    You should put the code up you're having trouble with :)
     
  4. ganesh_be

    ganesh_be

    Joined:
    Mar 10, 2020
    Posts:
    16
    I am having trouble with this code at line number 44
     
  5. davidnibi

    davidnibi

    Joined:
    Dec 19, 2012
    Posts:
    424
    Code (CSharp):
    1. inventory.AddItem(_item);
    I'm guessing _item doesn't exist when you're trying to add it because:

    Code (CSharp):
    1. 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.