❔ 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
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
here : D
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
Ok thanks
And please, if we are talking about such stuff, don't use public fields as a general rule, use properties
in unity fields are common practice
just making them inti properties brings no value
This is Unity.
Unity does not have coding conventions and is very anti-conventions
you cannot refactor autoproperties into field backed properties easily in unity, I think
[field: SerializableField]
But still, it's awful
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
Oh okay... just looked very ugly to me ^^
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
Unity is very ugly 🙂
Apparently xD
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.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 : (
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
?items do get added fine in the inventory when i don't include " GetComponent<InventoryItemController>().Count++"
you mean like this?
Yea for example
so i would end up something like this?
yea
that's way faster
still doesn't fix the issue tho
I'm no unity expert but your code seems kinda dodgy
yes ik
how come?
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)``would using a bool work in this case?
If you do it like that you would add the item on each iteration
I dont think you want that
I'd do it like that
ok so it does add the items now but it doesn't stack them
@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++"
what's the type of
itemType
?an enum
Like i said I'd suggest to rewrite the inventory system
well are you even sure that method gets called
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?
sure
ty i sent a message
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.