As the title says, I'm trying to create a method to delete items from my inventory system but it's not functioning as anticipated. Right now it's meant to delete a stack of a consumable item when I right click, and that does happen, except that it doesn't go lower than 1 and the item is not destroyed. The same with the other items that aren't consumable, they still have a stack size of 1 even though they aren't stackable but they aren't being destroyed on right click. Can anyone help me out please? Here's my script with the issue, specifically the RemoveItem method:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Inventory : MonoBehaviour {
GameObject inventoryPanel;
GameObject slotPanel;
ItemDatabase database;
public GameObject inventorySlot;
public GameObject inventoryItem;
int slotAmount;
public List- items = new List
- ();
public List
slots = new List();
void Start() {
database = GetComponent();
slotAmount = 15;
inventoryPanel = GameObject.Find("InventoryPanel");
slotPanel = inventoryPanel.transform.FindChild("SlotPanel").gameObject;
for(int i = 0; i < slotAmount; i++) {
items.Add(new Item());
slots.Add(Instantiate(inventorySlot));
slots[i].GetComponent().id = i;
slots[i].transform.SetParent(slotPanel.transform);
}
AddItem(0);
AddItem(0);
AddItem(1);
AddItem(0);
AddItem(2);
AddItem(2);
AddItem(2);
AddItem(3);
AddItem(3);
}
public void AddItem(int id) {
Item itemToAdd = database.FetchItemByID(id);
if(itemToAdd.Stackable && CheckIfItemIsInInventory(itemToAdd)) {
for(int i = 0; i < items.Count; i++) {
if(items[i].ID == id) {
ItemData data = slots[i].transform.GetChild(0).GetComponent();
data.amount++;
data.transform.GetChild(0).GetComponent().text = data.amount.ToString();
break;
}
}
} else {
for(int i = 0; i < items.Count; i++) {
if(items[i].ID == -1) {
items[i] = itemToAdd;
GameObject itemObj = Instantiate(inventoryItem);
itemObj.GetComponent().item = itemToAdd;
itemObj.GetComponent().slot = i;
itemObj.transform.SetParent(slots[i].transform);
itemObj.transform.position = Vector2.zero;
itemObj.GetComponent().sprite = itemToAdd.Sprite;
itemObj.name = itemToAdd.Title;
//next two lines before break handle stack index correction; remove if errors appear in the future
ItemData data = slots[i].transform.GetChild(0).GetComponent();
data.amount = 1;
break;
}
}
}
}
public void RemoveItem(int id) {
Item itemToRemove = database.FetchItemByID(id);
if(itemToRemove.Stackable && CheckIfItemIsInInventory(itemToRemove)) {
for(int j = 0; j < items.Count; j++) {
if(items[j].ID == id) {
ItemData data = slots[j].transform.GetChild(0).GetComponent();
data.amount--;
if(data.amount > 1) {
data.GetComponentInChildren().text = data.amount.ToString();
} else if(data.amount == 1) {
data.GetComponentInChildren().text = "";
} else if(data.amount < 1) {
itemToRemove.ID = -1;
}
}
}
}
}
bool CheckIfItemIsInInventory(Item item) {
for(int i = 0; i < items.Count; i++)
if(items[i].ID == item.ID)
return true;
return false;
}
}
Thanks in advance :)
↧