Answer the question
In order to leave comments, you need to log in
Item pickup doesn't work (doesn't appear in inventory)?
gives an error message:
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 [email protected]/Runtime/UI/Core/Button.cs:68)
UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at Library/PackageCache/[email protected] .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);
}
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question