so… I started to work on what I knew the least which was the inventory, almost everything is done except for the function of cumulative items, every time I pick up an item instead of increasing the quantity it doubles the item, the error seems to be between Inventory.cs and Item.cs it is not finding the item ID even if it’s the same and even if it seems right… (it seems like it’s going to end up being something obvious that I just haven’t detected)
public class Inventory : MonoBehaviour
{
public List<InventoryItem> inventoryItems = new List<InventoryItem>();
public Transform itemsParent;
public GameObject InventoryItemPrefab;
public void AddItem(Item item, int quantity)
{
Debug.Log($"Adding item with ID: {item.ID} and quantity: {quantity}");
foreach (var invItem in inventoryItems)
{
Debug.Log($"InventoryItem ID: {invItem.item.ID}, Quantity: {invItem.Quantity}");
}
//this line is the one that checks if the item's ID is the same of any items inside Inventory List
InventoryItem existingInventoryItem = inventoryItems.Find(i => i.item.ID == item.ID);
//and then here if theres an item with the same ID it updates the Amount instead of creating a new item on another slot
if (existingInventoryItem != null)
{
Debug.Log($"Item found. Current quantity: {existingInventoryItem.Quantity}");
existingInventoryItem.UpdateQuantity(existingInventoryItem.Quantity + quantity);
}
else
{
Debug.Log("Item not found. Adding new item.");
GameObject newItemObject = Instantiate(InventoryItemPrefab, itemsParent);
newItemObject.transform.SetSiblingIndex(0);
InventoryItem newInventoryItem = newItemObject.GetComponent<InventoryItem>();
newInventoryItem.SetItem(item, quantity);
inventoryItems.Add(newInventoryItem);
}
UpdateInventoryUI();
}
private void UpdateInventoryUI()
{
foreach (Transform child in itemsParent)
{
Destroy(child.gameObject);
}
foreach (InventoryItem inventoryItem in inventoryItems)
{
GameObject itemObject = Instantiate(InventoryItemPrefab, itemsParent);
InventoryItem inventoryItemComponent = itemObject.GetComponent<InventoryItem>();
inventoryItemComponent.SetItem(inventoryItem.item, inventoryItem.Quantity);
}
}
}
and then here it is the Item.cs this one only holds data that can be take by other scripts
public class Item : ScriptableObject
{
public string ItemName;
public int ID;
public string ItemDesc;
public int ItemValue;
public Sprite ItemIcon;
public ItemType itemType;
public List<ItemStat> itemStats = new List<ItemStat>();
public enum ItemType
{
Items,
KeyItems,
Armor,
Weapon,
SpecialEquipment,
Misc
}
public enum StatType
{
HP,
MP,
SP,
STR,
DEF,
AGI
}
[System.Serializable]
public class ItemStat
{
public StatType statType;
public int value;
}
public Item(int id,string name, ItemType type, string desc, Sprite icon, List<ItemStat> stats, int value)
{
ItemIcon = icon;
ItemName = name;
ItemDesc = desc;
ItemValue = value;
itemType = type;
itemStats = stats;
ID = id;
}
}