R1tr0
@R1tr0
Новичек в разработке игр, 15 лет

Не работает подбор предмета(не появляется в инвентаре)?

выдает ошибку:

NullReferenceException: Object reference not set to an instance of an object
Inventory.AddItem (Item itemToBeAdded, Item startingItem) (at Assets/Scripts/Inventory.cs:109)
Inventory.PickUp () (at Assets/Scripts/Inventory.cs:60)
UnityEngine.Events.InvokableCall.Invoke () (at <127e81e1cb3441cc97d26b1910daae77>:0)
UnityEngine.Events.UnityEvent.Invoke () (at <127e81e1cb3441cc97d26b1910daae77>:0)
UnityEngine.UI.Button.Press () (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/UI/Core/Button.cs:68)
UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/UI/Core/Button.cs:110)

вот скрипт:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class Inventory : MonoBehaviour
{
    public GameObject inventoryObject;
    public float distance = 2f;

    public Slot[] slots;

    public Slot[] equipSlots;

    void Start()
    {
        inventoryObject.SetActive(false);

        foreach (Slot i in slots)
        {
            i.CustomStart();
        }
        foreach (Slot i in equipSlots)
            i.CustomStart();
    }
    

    public void openInventory()
    {
        if (inventoryObject.activeSelf == false)
        {
            inventoryObject.SetActive(true);
           

        }
    }
    void Update()
    {
        
           
            
        
      
        foreach (Slot i in slots)
        {
            i.CheckForItem();
        }
        foreach (Slot i in equipSlots)
            i.CheckForItem();
    }

    public void PickUp()
    {
        
        
        
        
            
                AddItem(gameObject.GetComponent<Item>());

        
    }
    public int GetItemAmount(int id)
    {
        int num = 0;
        foreach (Slot i in slots)
        {
            if (i.slotsItem)
            {
                Item z = i.slotsItem;
                if (z.itemID == id)
                    num += z.amountInStack;
            }
        }
        return num;
    }

    public void RemoveItemAmount(int id, int amountToRemove)
    {
        foreach (Slot i in slots)
        {
            if (amountToRemove <= 0)
                return;

            if (i.slotsItem)
            {
                Item z = i.slotsItem;
                if (z.itemID == id)
                {
                    int amountThatCanRemoved = z.amountInStack;
                    if (amountThatCanRemoved <= amountToRemove)
                    {
                        Destroy(z.gameObject);
                        amountToRemove -= amountThatCanRemoved;
                    }
                    else
                    {
                        z.amountInStack -= amountToRemove;
                        amountToRemove = 0;
                    }
                }
            }
        }
    }

    public void AddItem(Item itemToBeAdded, Item startingItem = null)
    {
        int amountInStack = itemToBeAdded.amountInStack;
        List<Item> stackableItems = new List<Item>();
        List<Slot> emptySlots = new List<Slot>();

        if (startingItem && startingItem.itemID == itemToBeAdded.itemID && startingItem.amountInStack < startingItem.maxStackSize)
            stackableItems.Add(startingItem);

        foreach (Slot i in slots)
        {
            if (i.slotsItem)
            {
                Item z = i.slotsItem;
                if (z.itemID == itemToBeAdded.itemID && z.amountInStack < z.maxStackSize && z != startingItem)
                    stackableItems.Add(z);
            }
            else
            {
                emptySlots.Add(i);
            }
        }

        foreach (Item i in stackableItems)
        {
            int amountThatCanbeAdded = i.maxStackSize - i.amountInStack;
            if (amountInStack <= amountThatCanbeAdded)
            {
                i.amountInStack += amountInStack;
                Destroy(itemToBeAdded.gameObject);
                return;
            }
            else
            {
                i.amountInStack = i.maxStackSize;
                amountInStack -= amountThatCanbeAdded;
            }
        }

        itemToBeAdded.amountInStack = amountInStack;
        if (emptySlots.Count > 0)
        {
            itemToBeAdded.transform.parent = emptySlots[0].transform;
            itemToBeAdded.gameObject.SetActive(false);
        }
    }
}
  • Вопрос задан
  • 122 просмотра
Пригласить эксперта
Ответы на вопрос 1
freeExec
@freeExec
Участник OpenStreetMap
Гуглить - как отлаживать свой багнутый код.
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы