Skip to content

Instantly share code, notes, and snippets.

@Mike-Schvedov
Created April 23, 2025 10:23
Show Gist options
  • Select an option

  • Save Mike-Schvedov/b351fef48abd84900449cb6fba99404d to your computer and use it in GitHub Desktop.

Select an option

Save Mike-Schvedov/b351fef48abd84900449cb6fba99404d to your computer and use it in GitHub Desktop.
RemoveItem Method
public void RemoveItem(string itemName, int amountToRemove)
{
int remainingAmountToRemove = amountToRemove;
foreach (InventorySlot slot in slotList)
{
if (slot.itemInSlot != null && slot.itemInSlot.thisName == itemName)
{
int amountInSlot = slot.itemInSlot.amountInInventory;
int amountToTakeFromThisSlot = Mathf.Min(remainingAmountToRemove, amountInSlot);
slot.itemInSlot.amountInInventory -= amountToTakeFromThisSlot;
remainingAmountToRemove -= amountToTakeFromThisSlot;
if (slot.itemInSlot.amountInInventory == 0)
{
Destroy(slot.itemInSlot.gameObject);
slot.itemInSlot = null;
}
if (remainingAmountToRemove <= 0)
break;
}
}
if (remainingAmountToRemove > 0)
{
Debug.Log("Item not found or insufficient quantity in inventory");
}
ReCalculateList();
OnInventoryUpdate?.Invoke();
QuestManager.Instance.RefreshTrackerList();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment