So I wrote an inventory script, based on a list to store my items in. I’ve made a function to add the item, where I pass in an id, and an amount. In the function I check multiple things.
First I check if I even have anything in my inventory, if I don’t, it’s easy. I just add the given item as the very first item in my list.
Second I check if I already have the item in my inventory and if it’s stackable.
I do that with a bool function:
bool doesExist(int id)
{
for(int i = 0; i < inventoryCount; i++)
{
if(inventory*.itemID == id)*
{
existingSlot = i;
return true;
}
}
//existingSlot = inventoryCount -= 1;
//Reset inventoryCount
return false;
}
If I already have one of these items in my inventory and it’s stackable, I just add the given amt to the total amount for that inventory slot.
If however, I do not have this item in my inventory, or it’s just not stackable, I have an else statement. In the else statement I add a new item with the given id and amount. It all works great.
But…
I made a crafting system, where I find a recipe (premade), and then I loop through my inventory to check if I have all the item. If I do, I call the addItem function. This all works great the first time, but the second time I try to craft, it adds the item with the id one below (so say if I craft an axe with the id of 4, second time I craft the axe, it adds item with id of 3)
Crafting System:
public void SelectRecipe(string name)
{
for(int i = 0; i < recipes.recipes.Length; i++)
{
if(recipes.recipes*.name == name)*
{
selectedRecipe = recipes.recipes*.recipe;*
index = i;
//print(selectedRecipe[0]);
CheckforItems();
break;
}
}
}
void CheckforItems()
{
//print(“Checking for items”);
//print(inv.inventory[inv.inventoryCount-=1].itemName);
string[] first = selectedRecipe[0].Split(‘;’);
string[] second = selectedRecipe[1].Split(‘;’);
string[] third = selectedRecipe[2].Split(‘;’);
for (int i = 0;i < inv.inventoryCount; i++)
{
if (inv.inventory_.itemName == first[0] && inv.inventory*.itemAmt >= int.Parse(first[1]))
{
firstSlot = i;
requiredFirst = true;
}*_
if (requiredFirst && inv.inventory_.itemName == second[0] && inv.inventory*.itemAmt >= int.Parse(second[1]))
{
secondSlot = i;
requiredSecond = true;
}
if(requiredSecond && inv.inventory.itemName == third[0] && inv.inventory.itemAmt >= int.Parse(third[1]))
{
//Remove first item(s)
inv.inventory[firstSlot].itemAmt -= int.Parse(first[1]);
//inv.slots[firstSlot].transform.GetChild(0).transform.GetChild(0).GetComponent().text = inv.inventory[firstSlot].itemAmt.ToString();*_
//Remove second item(s)
inv.inventory[secondSlot].itemAmt -= int.Parse(second[1]);
//inv.slots[secondSlot].transform.GetChild(0).transform.GetChild(0).GetComponent().text = inv.inventory[secondSlot].itemAmt.ToString();
//Remove third item(s)
inv.inventory*.itemAmt -= int.Parse(third[1]);*
//inv.slots_.transform.GetChild(0).transform.GetChild(0).GetComponent().text = inv.inventory*.itemAmt.ToString();*_
//Add Item to inventory
AddItemToInv(recipes.recipes[index].id);
//print(recipes.recipes[index].id);
}
}
}
void AddItemToInv(int id)
{
//print(id);
inv.AddItem(id, 1);
requiredFirst = false;
requiredSecond = false;
firstSlot = 0;
secondSlot = 0;
thirdSlot = 0;
}
Inventory addItem function:
public void AddItem(int id, int amt)
{
//If we don’t have anything in our inventory
if(inventory.Count == 0)
{
inventory.Add(database.items[id -= 1]);
inventory[0].itemAmt = amt;
//Visual
slots.Add(Instantiate(slotPrefab));
slots[0].transform.SetParent(inventorySlots.transform);
GameObject itemObj = Instantiate(itemPrefab);
itemObj.transform.SetParent(slots[0].transform);
itemObj.GetComponent().sprite = database.items[id].itemIcon;
//Reset id
id = 0;
//print(id);
itemObj.transform.GetChild(0).GetComponent().text = inventory[0].itemAmt.ToString();
return;
}
//If we already have one of these items in our inventory
if (doesExist(id) && database.items[id-=1].stackAble)
{
print(id);
inventory[existingSlot].itemAmt += amt;
//Visual
slots[existingSlot].transform.GetChild(0).GetChild(0).GetComponent().text = inventory[existingSlot].itemAmt.ToString();
id = 0;
return;
}
else
{
//print(id);
inventory.Add(database.items[id -= 1]);
//Reset id
//id += 1;
inventory[inventoryCount].itemAmt = amt;
//print(database.items[id].itemName);
//Visual
slots.Add(Instantiate(slotPrefab));
slots[inventoryCount].transform.SetParent(inventorySlots.transform);
GameObject itemObj = Instantiate(itemPrefab);
itemObj.transform.SetParent(slots[inventoryCount].transform);
itemObj.GetComponent().sprite = database.items[id].itemIcon;
//print(id);
//Reset id
id = 0;
itemObj.transform.GetChild(0).GetComponent().text = inventory[inventoryCount].itemAmt.ToString();
return;
}
}
The error seems to be located in the last part of the addItem function. The id changes, and I have no idea why. Can anyone help me pls? :3