Hello, im following this video https://www.youtube.com/watch?v=oJAE6CbsQQA
How do i create function that removes item on click.
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using UnityEngine;
using UnityEngine.EventSystems;
using static UnityEditor.Progress;
public class InventoryManager : MonoBehaviour
{
public int maxStackedItems = 99;
public InventorySlot[] inventorySlots;
public ToolBarSlot[] toolbarSlots;
public GameObject inventoryItemPrefab;
public GameObject toolbarItemPrefab;
int selectedSlot = -1;
public InventorySlot GetSlotByIndex(int index) => inventorySlots[index];
public bool AddItem(ItemSO item)
{
for (int i = 0; i < toolbarSlots.Length; i++)
{
InventorySlot slot = inventorySlots[i];
InventoryItem itemSlot = slot.GetComponentInChildren<InventoryItem>();
if (itemSlot != null && itemSlot.item == item && maxStackedItems < 99)
{
itemSlot.count++;
itemSlot.RefreshCount();
return true;
}
}
for (int i = 0; i < inventorySlots.Length; i++)
{
InventorySlot slot = inventorySlots[i];
InventoryItem itemSlot = slot.GetComponentInChildren<InventoryItem>();
if (itemSlot == null)
{
SpawNewItem(item, slot);
return true;
}
}
return false;
}
public bool AddItemToToolbar(ItemSO item)
{
for (int i = 0; i < toolbarSlots.Length; i++)
{
ToolBarSlot slot = toolbarSlots[i];
ToolBarItem itemSlot = slot.GetComponentInChildren<ToolBarItem>();
if (itemSlot == null)
{
SpawNewItemToolBar(item, slot);
return true;
}
}
return false;
}
public void Update()
{
}
void SpawNewItem(ItemSO item, InventorySlot slot)
{
GameObject newItemGameObject = Instantiate(inventoryItemPrefab, slot.transform);
InventoryItem inventoryItem = newItemGameObject.GetComponent<InventoryItem>();
inventoryItem.InitialiseItem(item);
}
void SpawNewItemToolBar(ItemSO item, ToolBarSlot slot)
{
GameObject newItemGameObject = Instantiate(toolbarItemPrefab, slot.transform);
ToolBarItem inventoryItem = newItemGameObject.GetComponent<ToolBarItem>();
inventoryItem.InitialiseItem(item);
}
public void RemoveAt(ItemSO item, int slotIndex)
{
for (int i = 0; i < inventorySlots.Length; i++)
{
InventorySlot slot = inventorySlots[slotIndex];
InventoryItem itemSlot = slot.GetComponentInChildren<InventoryItem>();
if (itemSlot != null)
{
if (itemSlot.item == item)
{
Destroy(itemSlot.gameObject);
}
}
}
}
public void RemoveAtIndex(int slotIndex)
{
for (int i = 0; i < inventorySlots.Length; i++)
{
InventorySlot slot = inventorySlots[i];
InventoryItem itemSlot = slot.GetComponentInChildren<InventoryItem>();
if (itemSlot != null)
{
}
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class InventorySlot : MonoBehaviour, IDropHandler, IPointerClickHandler
{
public InventoryManager inventory;
public void OnDrop(PointerEventData eventData)
{
if (transform.childCount == 0)
{
InventoryItem inventoryItem = eventData.pointerDrag.GetComponent<InventoryItem>();
inventoryItem.parentAfterDrag = transform;
}
}
public void OnPointerClick(PointerEventData eventData)
{
InventoryItem inventoryItem = eventData.pointerDrag.GetComponent<InventoryItem>();
if (inventoryItem.item.type == ItemType.Laser)
{
inventory.AddItemToToolbar(inventoryItem.item);
inventory.RemoveAt(inventoryItem.item, inventoryItem.index);
}
}
}