So I've been working on an inventory script, I'm trying to test it out. Everything works fine except for the `AddItem` method, shown below.
public void AddItem(Item item) {
// Check if a stack is already there for us to stack the item with
for(int i = 0; i <= 25; i++) {
// If nothing is in this inventory slot, check the next one.
if(Items[i] == null) {
continue;
}
// If stack is found, increase the stack size.
if(Items[i].ContainingItem.Name == item.Name) {
Items[i].StackSize++;
return;
}
}
// Create a new item stack.
ItemStack st = new ItemStack(item, 1);
// If no stacks of the same item is found, then
// check for space in inventory to put a new item stack into.
for(int i = 0; i <= 25; i++) {
// If item in inventory slot is null (no itemstack inside)
if(Items[i] == null) {
// Add a new item stack.
Items[i] = st;
return;
}
}
/*foreach(ItemStack i in Items) {
Debug.Log(i.ContainingItem.Name);
}*/
}
Here's my problem: whenever I add an item, it adds nothing, and the `Debug.Log` stuff doesn't print anything, suggesting that nothing is being added to the array. Oddly enough, if I remove the 2 `return` statements the entire inventory is litterally filled with nothing but that item. Will somebody please fix this, or atleast explain why this is happening?
**EDIT: ** I've only included the AddItem method, I assure you everything else is working as expected.
**EDIT: **
Items is a built-in array, I thought to use this because it would work very well with the inventory slots stuff. ItemStack is a class with 3 properties: ContainingItem, StackSize, and MaxStackSize. Stacksize is just a public variable (I know, it's bad, I'm sorry :P)
When an item that doesn't exist in inventory is added into a non-full inventory it should just add that item.
When an item that does exist in inventory is added it should increase stack size, I haven't checked if that works yet.
When the inventory is full, the item is dismissed and never seen from again. If you want the entire script here it is, but it's a bit huge and messy, I'll try to comment as much as I can.
Pastebin link to my inventory script is here: http://pastebin.com/kxupp6Ap
Item here: http://pastebin.com/YwB6ru29
and ItemStack here: http://pastebin.com/4YRSc5Bz
↧