I'm working on an Inventory System with a Crafting System. When I first made it it was working, it crafted items if the player had enough resources in Inventory to craft it and then removed the items that were used to craft the item and add the item we just crafted. But now the part where it should delete items that were used to craft the object doesn't work.
Function where everything starts:
public bool CraftItem() {
//if CanCraft returned false we return false from function
if(!CanCraft()) {
return false;
}
//start crafting
ParentCraftingSlot.StartCrafting();
//call RemoveIngredientsFromInventory to remove item from inventory
RemoveIngredientsFromInventory();
//return true
return true;
}
RemoveIngredientsFromInventory function:
//remove ingredients from inventory
//because we don't want to have the ingredients after we crafted something
private void RemoveIngredientsFromInventory() {
foreach(Ingredient ingredient in ingredients) {
//call RemoveItems function with ingredient item and amount
Inventory.instance.RemoveItems(ingredient.item, ingredient.amount);
}
}
RemoveItems function:
//RemoveItems from inventory
public void RemoveItems(Item item, int amount) {
//loop through amount of items
for(int i = 0; i < amount; i++) {
//remove item from Inventory
RemoveItem(item);
}
}
and then RemoveItem function (i have it as a different function because I need to remove the item from the list and then invoke the changes so it's displayed and then I don't have to write it every time I just have to call the function):
//Remove Item function
public void RemoveItem(Item item) {
//remove item from IntentoryItemList list
InventoryItemList.Remove(item);
onItemChange.Invoke();
}
I tried to write something into console inside the RemoveItem function and it wrote it into console but for some reason, it didn't remove the ingredients.