C
C#2y ago
Sangit

❔ Inventory item stacking

i am working on a inventory system in unity where i am trying to add item stacking, however i am having issues where its not displaying the correct number of items that are stacked. , i managed to spot something which might be the main reason why items are not stacking properly. so whenever i am incrementing items, the item count goes up as expected however when i click on the inventory icon, the values reset for some reason not really sure how to fix it.
18 Replies
Sangit
SangitOP2y ago
here is the issue
pip
pip2y ago
that is some really rough code to read firstly you are calling "incrementcounter" in so many places that i have no idea how your program even logs the correct amount it most likely has something to do with the "Count" component on your display look at ListItems are you actually setting the correct Count(internal variable) to the item's count display?
Sangit
SangitOP2y ago
Sangit
SangitOP2y ago
yes i am let me triple check
Sangit
SangitOP2y ago
yea i am
pip
pip2y ago
so you're only updating the text when the object is initialized create a new function "UpdateText" with that same code and call that when you open the inventory easy
Sangit
SangitOP2y ago
like this ?
Sangit
SangitOP2y ago
i did this before but it didnt help
pip
pip2y ago
maybe set it active first then set the text
Sangit
SangitOP2y ago
@pip should i call the UpdatedText here in the Add function?
pip
pip2y ago
sure, try it you know your code better than i
Sangit
SangitOP2y ago
Ahh gives me a null reference error I tried putting "UpdatedText" inside update function and still same issue I genuinely don't see why the count value gets reset
Anchy
Anchy2y ago
why not just have Item have a property called Count which you could implement instead of relying on this inventory controller
/// <summary>
/// adding items to the list
/// </summary>
/// <param name="item"></param>
public void Add(Item newItem)
{
foreach (Item item in Items)
{
if (item.itemType == newItem.itemType)
{
if(item.Count < Item.MaxStackSize)
item.AddCount(1);

return;
}
}

Items.Add(newItem);
}
/// <summary>
/// adding items to the list
/// </summary>
/// <param name="item"></param>
public void Add(Item newItem)
{
foreach (Item item in Items)
{
if (item.itemType == newItem.itemType)
{
if(item.Count < Item.MaxStackSize)
item.AddCount(1);

return;
}
}

Items.Add(newItem);
}
you could do something like this you may need to implement further logic to account for newItem stack size and compare the current item stack size to see if you need to create a new stack but you get the idea
Sangit
SangitOP2y ago
so i did something similar where i have count implemented in my inventory manager instead of relying on inventory controller so that i dont have to worry about inheriting issues and the issue i have right now is that items no longer stack when adding them, the count value in the log is correct since there are 8 items in the list but they still display as 1.
Anchy
Anchy2y ago
the count or stack size should really be the responsibility of the item
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server