C
C#2y ago
Sangit

❔ Item stacking in unity c#

I am trying to get my items to stack in my inventory, so i have added a counter to my script which displays the amount of items currently in the stack, i have added that to my item prefab and my InventoryitemController script. however i am a bit lost on how to actually increment the count value each time a item is added and to stack them properly. This is what i have so far (edited)
40 Replies
Anton
Anton2y ago
name your variables properly you got both yourself and the readers confused as to what that's supposed to do and post the entire files I can't tell what scripts these snippets are from
Sangit
SangitOP2y ago
here : D
Anton
Anton2y ago
format your code before figuring out the issue remove trailing empty lines and fix the indentation your ide has the feature "format document" if you don't know how to do it, look it up if these files are on github, you can just post the github linke also if you're gonna put it on github, use a gitignore file meant for unity
Sangit
SangitOP2y ago
Ok thanks
Turwaith
Turwaith2y ago
And please, if we are talking about such stuff, don't use public fields as a general rule, use properties
Anton
Anton2y ago
in unity fields are common practice just making them inti properties brings no value
Buddy
Buddy2y ago
This is Unity. Unity does not have coding conventions and is very anti-conventions
Anton
Anton2y ago
you cannot refactor autoproperties into field backed properties easily in unity, I think
Buddy
Buddy2y ago
[field: SerializableField] But still, it's awful
Anton
Anton2y ago
yeah I mean what if you happen to need an explicit backing field after that what's the name of the autogenerated backing field? you need that name to be able to apply FormerlySerializedAs
Turwaith
Turwaith2y ago
Oh okay... just looked very ugly to me ^^
Dusty
Dusty2y ago
And what isn't working? You should add a return statement inside your if in the foreach loop if the count was incremented Otherwise u increment the count and add the item aswell? Not really sure what the Items variable is
Pobiega
Pobiega2y ago
Unity is very ugly 🙂
Turwaith
Turwaith2y ago
Apparently xD
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.
Sangit
SangitOP2y ago
hey guys sorry for the late reply but here are my updated files so i am trying to loop through the inventoryitems in itemConnent and if the item in the inventory is the same type of item that's being added then simply increment the "Count" variable. "Count" is a text component in the item prefab which shows the number of stacked them in the inventory . @AntonC @Buddy @Dusty. the issue i am having right now is that the items don't show up in the inventory when i add them : (
Sangit
SangitOP2y ago
Dusty
Dusty2y ago
GetComponent<InventoryItemController>().Count++; you should move the GetComponent call into the constructor and cache the instance Maybe I don't see it but you're never adding the item to ItemContent only to Items ?
Sangit
SangitOP2y ago
items do get added fine in the inventory when i don't include " GetComponent<InventoryItemController>().Count++"
Sangit
SangitOP2y ago
you mean like this?
Dusty
Dusty2y ago
Yea for example
Sangit
SangitOP2y ago
so i would end up something like this?
Dusty
Dusty2y ago
yea that's way faster
Sangit
SangitOP2y ago
still doesn't fix the issue tho
Dusty
Dusty2y ago
I'm no unity expert but your code seems kinda dodgy yes ik
Sangit
SangitOP2y ago
how come?
Dusty
Dusty2y ago
What I would do is the following: 1. Create a class called Item 2. Add a Count property and your other properties to the class 3. Create a list or hashset called inventoryItems for example Like you did, loop over the list and once there's the same item type, increment the count on that instance. And your UI should call methods of the InventoryManager, like GetItemCount() which would return for a specific item That would allow you to decouple UI and logic Your UI & logic seems pretty mixed up, you suddenly increment some "weird" count in another class that's non of it's business For a simple inventory like you have atleast, otherwise I'd add separate class called ItemStack`´ for example which holds a list of items and would expose a count property. But I don't think that's needed here The issue I've just seen is that you loop over but once u compared the 1st item you go into the else case. You just loop over the whole list and if you enter the if clause (so after the Count++) you return and below your foreach you do Items.Add(item)``
Sangit
SangitOP2y ago
would using a bool work in this case?
Dusty
Dusty2y ago
If you do it like that you would add the item on each iteration I dont think you want that
public void Add(Item item)
{

foreach (Item InventoryItems in ItemContent)
{
if (InventoryItems.itemType == item.itemType)
{
inventoryController.Count++;
return;
}
}

Items.Add(item);
}
public void Add(Item item)
{

foreach (Item InventoryItems in ItemContent)
{
if (InventoryItems.itemType == item.itemType)
{
inventoryController.Count++;
return;
}
}

Items.Add(item);
}
I'd do it like that
Sangit
SangitOP2y ago
ok so it does add the items now but it doesn't stack them
Sangit
SangitOP2y ago
@Dusty so upon some debuging using breakpoints it seems that program never gets inside the foreach statement (Line 46) so it never reads "inventoryController.Count++"
Anton
Anton2y ago
what's the type of itemType?
Sangit
SangitOP2y ago
an enum
Dusty
Dusty2y ago
Like i said I'd suggest to rewrite the inventory system
Anton
Anton2y ago
well are you even sure that method gets called
Sangit
SangitOP2y ago
Add method is getting called But the if statement isn't being read @Dusty hey can i dm you with a question related to this?
Dusty
Dusty2y ago
sure
Sangit
SangitOP2y ago
ty i sent a message
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