- Home /
The question is answered, right answer was accepted
[SOLVED] Inventory Stacking Problem [C#]
My goal is to create inventory based on the following rules: 1. When you pick up an item, check if it is already in our inventory 2. If it's not, instantiate the item in the next slot in inventory and add it to the list 3. If it's already added, increase the amount of the existing item. Here's the code:
Item itemObj;
bool goodToPickUp = true;
int n = 0;
public void AddItem(Item item)
{
if (itemList.Contains(item)) // Check if we already have the item
{
goodToPickUp = false;
}
else
{
goodToPickUp = true;
}
// Adds an item if we don't have it in our inventory yet
if (goodToPickUp)
{
itemObj = Instantiate(item, slots[n].transform, false); // Instantiate the item and store it in itemObj variable
n++;
itemList.Add(item);
}
else
{
for (int i = 0; i < itemList.Count; i++)
{
if (itemList[i].Equals(item))
{
itemObj.amount++;
// itemObj doesn't change, so no matter what item will be picked up now, amount of the last will be increased
}
}
}
}
The problem: itemObj doesn't update if the item has already been in our inventory. As a result, only the amount of the last item picked up is increased. I've tried to put in in words as well as I could, please ask questions if something is unclear.
EDIT:
Here's a video that might shed some light on what I'm trying to fix
you're only assigning itemObj a value when you instantiate something. is you want to use it in the else case, assign it the value you found.
Answer by Gecuit · Nov 03, 2018 at 02:40 PM
Hah, I just managed to fix it. My solution was to create a separate list for instances and separate for prefabs.
public List<Item> itemList = new List<Item>(); // List storing of all objects picked up
public List<Item> itemObjList = new List<Item>(); // List storing instances of all objects picked up
Then I changed if statement in AddItem(Item) to this:
// Adds an item if we don't have it in our inventory yet
if (goodToPickUp)
{
itemList.Add(item);
itemObjList.Add(Instantiate(item, slots[n].transform, false)); // Instantiate the item and add it to the list
n++;
}
else
{
for (int i = 0; i < itemList.Count; i++)
{
if (itemList[i].Equals(item))
{
itemObjList[i].amount++; // Increases amount of the instance
}
}
}
@EgoAnt your answer kind of guided me what to do, but it's not correct itself. Thank you for your help anyway :)
Awesome! I was just doing a quick code sample to show exactly this. Glad you solved it!
Answer by AaronXRDev · Nov 02, 2018 at 10:29 PM
Looks like you just need to change:
itemObj.amount++;
to:
itemList[i].amount++;
If that doesn't work, do you mind posting the class code for Item here?
Unfortunately, that didn't solve the problem, because itemList contains prefabs, and not instances. Therefore it increases the amount of prefab, without doing anything in the scene. And here's Item class which you asked for:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class Item : $$anonymous$$onoBehaviour
{
public int amount = 1;
public string itemName;
// Update UI
void Update()
{
transform.GetChild(0).GetComponent<UnityEngine.UI.Text>().text = amount + "";
}
}
The first solution that comes to my $$anonymous$$d would be to change itemList.Add(item);
line to itemList.Add(itemObj);
in the AddItem(Item) method. However then, if (itemList.Contains(item))
will always return false, because the list will be made up of instances and not prefabs.
And if I change the if statement to check for itemObj ins$$anonymous$$d of item, it won't work because itemObj hasn't yet been assigned.
For clarification itemObj is an instance of item. I start to think that I should rewrite the entire inventory system because this one got too complicated.
Follow this Question
Related Questions
Item Database 3 Answers
[C#]Inventory script help. 3 Answers
Best Way to do Regular Items and Weapons/Intractable/Special Items. 1 Answer