Hey,
I started working on a simple inventory system when I was still using 2019.1. It worked like a charm, exactly like I expected it to. Then I updated to 2019.4, and it stopped working. No error or anything, it seemed like something in my code simply stopped responding. From experience, update-related issues usually involve all hell breaking loose and errors piling up by the dozen in the console, but this predicament here was eerily quiet. I let it sit for a few weeks because I didn’t have much free time on my hands and eventually updated to 2020.1 when I came back to the project, but still no luck.
After further investigation, I realized the problem came from a List. See, my inventory system is centered around a List of “InventorySlots”; those hold a reference to an item object and the amount in possession. Every time the player picks up an item, it adds an InventorySlot to that inventory List. Simple enough, and yet…
Here’s the “unresponsive” code snippet from the Inventory class, which also holds the List of InventorySlots:
public void AddItem(Item item, int amount)
{
InventorySlot newSlot = new InventorySlot(item, amount);
Debug.Log(newSlot.amount + "x " + newSlot.item.name);
inventorySlots.Add(newSlot);
}
The debug log returns exactly what I expect it to: a string with the item name, and an int with the item amount. My new slot that’s being generated is not null, so the problem doesn’t come from the code that is responsible for passing on that data. It’s as if my Add line simply did not exist. No matter how many items I pick up, nothing gets added to the list and its size remains stubbornly stuck at zero.
I find it difficult to believe that a mere update would break such a simple system, but I have no clue to what the actual source of the issue might be. Maybe someone familiar with List troubles might be able to nudge me into the right direction?
Cheers.
Gotta share some of your other code surrounding this “inventorySlots” variable. Hard to know without that.
1 Like
Thank you for your very quick response!
Well, there is the initialization:
public List<InventorySlot> inventorySlots = new List<InventorySlot>();
And that’s pretty much it.
Here’s the InventorySlot class; just two fields plus a constructor.
public class InventorySlot
{
public Item item;
public int amount;
public InventorySlot(Item item, int amount)
{
this.item = item;
this.amount = amount;
}
}
Ok but surely you’re reading this list somewhere? And where are you calling AddItem from?
1 Like
The inventory’s interface is handled by another script that I have disabled for the time being, specifically so I could rule out the problem coming from there. Other than that, there is no class that I can recall to be making any reference to the Inventory class whatsoever. The system is very much still in its early phase of development.
Here’s the code snippet that calls AddItem, from the Item class:
public void Interact()
{
Inventory.instance.AddItem(RetrieveItem());
Destroy(gameObject);
}
public Item RetrieveItem()
{
Item retrieved = null;
foreach (Item item in GameManager.instance.itemDatabase)
{
if (item.name == this.name)
{
retrieved = item;
}
}
return retrieved;
}
Interact is called when the player clicks an item. Inventory.instance is the Inventory class singleton. RetrieveItem returns the item from the Item database. I figured the object-destroying bit might be troublesome, but removing it does not solve the issue. Anyway, the code here is behaving as expected, so I don’t think it’s the culprit.
I’m assuming you think the code for my inventory might be overridden somehow, from somewhere else? Now that I think about it, is it possible that maybe the inspector is in some way (that I don’t know of) responsible for that overriding?
Actually my thoughts are more along the lines of you have more than one Inventory object. What does your singleton implementation look like?
1 Like
How are you concluding that nothing is happening, and that your inventorySlots length is stuck at 0?
Do you have an ingame UI, and it’s not updating, are you using the editors Inspector?
2 Likes
… Of course. That’s spot on. I had duplicated my player controller to do some tests and put it on the side (and forgot about it) to work on something else, and of course the duplicated object came with all sorts of components — including a still very active Inventory class. The code was dealing with that instance, not the one I expected it to. It’s a bit embarrassing to be honest, but at least the problem is solved and I hopefully won’t get fooled again.
Thanks a lot, your help was very much appreciated!
1 Like