I have nested if’s in side a for loop which is used to add an item to a list. But when i first add an item with an amount of 4 an then add a second item with 3 it should make 7, but come out with 6?
I tried putting ‘print()’ functions around the function, but i couldnt see anything wrong?
Code:
public void AddItem(Item newItem){
int itemAmount = newItem.currentStack;
if(PlayerInventory.Inventory.Count > 0){
foreach(Item item in PlayerInventory.Inventory){
if(item.ID == newItem.ID){
if(item.currentStack < item.maxStack){
int availableSpace = item.maxStack - item.currentStack;
if(itemAmount <= availableSpace){
print (item.currentStack + itemAmount + " :Final Amount");
item.currentStack += itemAmount;
itemAmount = 0;
} else{
item.currentStack = item.maxStack;
itemAmount -= availableSpace;
}
}
}
}
if(itemAmount > 0){
newItem.currentStack = itemAmount;
PlayerInventory.Inventory.Add(newItem);
}
} else{
PlayerInventory.Inventory.Add(newItem);
}
print ("-------------------------");
}