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 Error

Discussion in 'Scripting' started by amp3670, Apr 7, 2020.

  1. amp3670

    amp3670

    Joined:
    Apr 1, 2020
    Posts:
    10
    I'm creating a simple child app in Unity. The function that I am making now is a shop function.
    When child buys items at the shop, I want to make sure that child can't buy them again.
    Once an item is purchased at a shop, the item's information is stored in the Inventory.
    IsInven() is to see if there are any items that child wants to purchase in the Inventory already, and if there are items in the Inventory that child wants to purchase, attempt to disable them.
    However, a Null Reference Exception error occurred. I want to check which part is wrong.

    The following is the content of the error.

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6. using System;
    7. public class Store_Sky : MonoBehaviour
    8. {
    9.     public ItemBuffer itemBuffer;
    10.     public Transform slotRoot;
    11.     public Action<ItemProperty> onClickBuy;
    12.     private List<Slot> slots;
    13.     public Inventory inven;
    14.     //게임데이터 파일 만들면 삭제하기
    15.     int userStar = 4;
    16.     void FixedUpdate()
    17.     {
    18.         slots = new List<Slot>();
    19.         int slotCnt = slotRoot.childCount;
    20.         for(int i = 0; i < slotCnt; i++)
    21.         {
    22.             var slot = slotRoot.GetChild(i).GetComponent<Slot>();
    23.             if(slot.item.starNum <= userStar)
    24.             {
    25.                 slot.SetItem(itemBuffer.items[i+21]);
    26.             }
    27.             else
    28.             {
    29.                 slot.GetComponent<Button>().interactable = false;
    30.                 onClickBuy = null;
    31.             }
    32.             slots.Add(slot);
    33.         }
    34.     }
    35.     public void OnclickBuy(Slot slot)
    36.     {
    37.         if (userStar >= 0 && IsInven(slot) == false)
    38.         {
    39.             onClickBuy?.Invoke(slot.item);
    40.             userStar -= slot.item.starNum;
    41.             Debug.Log(userStar);
    42.         }
    43.     }
    44.     private bool IsInven(Slot slot)
    45.     {
    46.         var invenList = inven.slots.FindAll(t =>
    47.         {
    48.             return t.item != null || t.item.itemname != string.Empty;
    49.         });
    50.         if(invenList != null)
    51.         {
    52.             var isInven = invenList.Find(p =>
    53.             {
    54.                 return p.item.itemID == slot.item.itemID;
    55.             });
    56.             return isInven != null || isInven.item.itemname != string.Empty;
    57.         }
    58.         else
    59.         {
    60.             return false;
    61.         }
    62.     }
    63. }
    64.  
    65. }
     
  2. kasztelan

    kasztelan

    Joined:
    Nov 3, 2014
    Posts:
    11
    Code (CSharp):
    1. return isInven != null || isInven.item.itemname != string.Empty;
    I think you want to use && instead of || here