Search Unity

Method does not see the object (NullReferenceException)

Discussion in 'Scripting' started by bkjvkk, Oct 20, 2019.

  1. bkjvkk

    bkjvkk

    Joined:
    Oct 20, 2019
    Posts:
    3
    I am following some video tutorial, on yt, checked it 3 times and i couldn't find mistake in my code but anytime i try to press on "item" i get


    NullReferenceException: Object reference not set to an instance of an object
    ItemSlot.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at Assets/Scripts/ItemSlot.cs:39)
    UnityEngine.EventSystems.ExecuteEvents.Execute (UnityEngine.EventSystems.IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:50)
    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()

    and here is also the code:

    Code (CSharp):
    1. [CreateAssetMenu]
    2. public class Item : ScriptableObject
    3. {
    4.     public string itemName;
    5.     public Sprite icon;
    6. }
    Code (CSharp):
    1. public class ItemSlot : MonoBehaviour, IPointerClickHandler
    2. {
    3.     [SerializeField] public Image image;
    4.  
    5.     public event Action<Item> OnRightClickEvent;
    6.  
    7.     private Item _item;
    8.     public Item Item
    9.     {
    10.         get {return _item;}
    11.         set
    12.         {
    13.             _item = value;
    14.  
    15.             if(_item == null)
    16.             {
    17.                 image.enabled = false;
    18.             }
    19.             else
    20.             {
    21.                 image.sprite = _item.icon;
    22.                 image.enabled = true;
    23.             }
    24.         }
    25.     }
    26.  
    27.     public void OnPointerClick(PointerEventData eventData)
    28.     {
    29.         if (eventData != null && eventData.button == PointerEventData.InputButton.Right)
    30.         {
    31.             Debug.Log(Item.name);
    32.             if (Item != null && OnRightClickEvent != null)
    33.                 OnRightClickEvent(Item);
    34.         }
    35.     }
    36.  
    37.     protected virtual void OnValidate()
    38.     {
    39.         if(image == null)
    40.         {
    41.             image = GetComponent<Image>();
    42.         }
    43.     }
    44. }
    I added whole code in the attachments
     

    Attached Files:

  2. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    You null reference is on line 39. As you've excluded the using lines, I would guess that yourOnRightClickEvent action is null.
     
  3. bkjvkk

    bkjvkk

    Joined:
    Oct 20, 2019
    Posts:
    3
    But why this is null? This accepts "Item" and get's "Item", to get to that point it has to exist
     
  4. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    Maybe it's the item. Which is line 39?
     
  5. bkjvkk

    bkjvkk

    Joined:
    Oct 20, 2019
    Posts:
    3
    Code (CSharp):
    1.     public void OnPointerClick(PointerEventData eventData)
    2.     {
    3.         if (eventData != null && eventData.button == PointerEventData.InputButton.Right)
    4.         {
    5.             Debug.Log(Item.name);
    6.             if (Item != null && OnRightClickEvent != null)
    7.                 OnRightClickEvent(Item); //that's 39
    8.         }
    9.     }