Search Unity

Help please! Error: NullReferenceException: Object reference not set to an instance of an object Inv

Discussion in 'Scripting' started by mykzgonzalez, Feb 20, 2018.

  1. mykzgonzalez

    mykzgonzalez

    Joined:
    Feb 20, 2018
    Posts:
    8
    This is the code that I have..

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.EventSystems;
    5. using UnityEngine.UI;
    6. public class Inventory : MonoBehaviour {
    7.     public GameObject currentSelectedSlot { get; set; }
    8.     public GameObject previousSelectedSlot { get; set; }
    9.     private GameObject slots;
    10.     public GameObject itemDisplayer { get; private set; }
    11.     private bool IsPointerOverUIObject()
    12.     {
    13.         PointerEventData eventDataCurrentPosition = new PointerEventData(EventSystem.current);
    14.         eventDataCurrentPosition.position = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
    15.         List<RaycastResult> results = new List<RaycastResult>();
    16.         EventSystem.current.RaycastAll(eventDataCurrentPosition, results);
    17.         return results.Count > 0;
    18.     }
    19.     void Start()
    20.     {
    21.         InitializedInventory();
    22.     }
    23.     void Update()
    24.     {
    25.         HideDisplay();
    26.         SelectedSlot();
    27.     }
    28.     void InitializedInventory()
    29.     {
    30.         slots = GameObject.Find("Slots");
    31.         itemDisplayer = GameObject.Find("ItemDisplayer");
    32.         itemDisplayer.SetActive(false);
    33.         foreach (Transform slot in slots.transform)
    34.         {
    35.             slot.transform.GetChild(0).GetComponent<Image>().sprite =
    36.                 Resources.Load<Sprite>("Inventory Items/empty_item");
    37.         }
    38.     }
    39.     void SelectedSlot()
    40.     {
    41.         foreach (Transform slot in slots.transform)
    42.         {
    43.             if(slot.gameObject == currentSelectedSlot && slot.GetComponent<Slot>().ItemProperty == Slot.property.usable)
    44.             {
    45.                 slot.GetComponent<Image>().color = new Color(.9f, .4f, .6f, 1);
    46.             }
    47.             else if (slot.gameObject == currentSelectedSlot && slot.GetComponent<Slot>().ItemProperty == Slot.property.displayable)
    48.             {
    49.                 slot.GetComponent<Slot>().DisplayItem();
    50.             }
    51.             else
    52.             {
    53.                 slot.GetComponent<Image>().color = new Color(1, 1, 1, 1);
    54.             }
    55.         }
    56.     }
    57.     void HideDisplay()
    58.     {
    59.         if(Input.GetMouseButtonDown(0) && !UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject())
    60.         {
    61.             itemDisplayer.SetActive(false);
    62.             if(currentSelectedSlot.GetComponent<Slot>().ItemProperty == Slot.property.displayable)
    63.             {
    64.                 currentSelectedSlot = previousSelectedSlot;
    65.                 previousSelectedSlot = currentSelectedSlot;
    66.             }
    67.         }
    68.     }
    69. }
    The problem Unity is sending me is at Line 71..

    Code (CSharp):
    1.   if(currentSelectedSlot.GetComponent<Slot>().ItemProperty == Slot.property.displayable)
    2.             {
    3.                 currentSelectedSlot = previousSelectedSlot;
    4.                 previousSelectedSlot = currentSelectedSlot;
    5.             }
    tried to read and read but can't find the correct way//
    Anyone knows how to fix this? thanks. :)
     
    Last edited: Feb 20, 2018
  2. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,618
  3. mykzgonzalez

    mykzgonzalez

    Joined:
    Feb 20, 2018
    Posts:
    8
    Yeah Sorry about that.. I'm watching a tutorial about a game.. on his Video, he got the same error that I have. the problem is on his video the error is not stopping him and just goes on. So I wonder..
     
  4. Which one is the line 71?

    If the first line of the second code-piece, you probably don't have "Slot" component on this object or you don't have anything in the currentSelectedSlot.
     
  5. mykzgonzalez

    mykzgonzalez

    Joined:
    Feb 20, 2018
    Posts:
    8
    sorry that was a typo. it was 61 and I already send the code witch triggers the error. When i removed that line of code (2nd code) all works properly.
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Check everything on the line that can be null and Debug.Log if it is. Track down why (if it shouldn't be) or don't perform the code if it could be null (or not null) .. if it's null ;)
     
  7. mykzgonzalez

    mykzgonzalez

    Joined:
    Feb 20, 2018
    Posts:
    8
    Thank you so much guys. The problem has been fixed. ^_^ The problem was I did not initialize that the current slot must have an empty object. Again, Thank you so much for your help.