In my game, I have an inventory system. Items can get added to the inventory, and each item slot shows the quantity of an item, but I’m trying to make it that if an item is selected in the inventory (with “isOn”), and if the player uses the “Use/Equip” button, then the quantity of the item decreases by one, and the child of the item slot “text_ItemAmount” (which shows the quantity of the item) updates to show the correct quantity. However, if I have more than one items in my inventory, and I select the first item and click the “Use/Equip” button, the item decreases in quantity but it isn’t shown in the game, and the “text_ItemAmount” stays to the previous quantity it was. However, If I have the very last item in the inventory selected, and I press the “Use/Equip” button, the quantity of the item decreases and the inventory slot updates it’s child “text_ItemQuantity” (which shows the item’s quantity in game) correctly, but this only works for the very last item, and if I try to do this for previous items, it won’t set the text to the correct quantity.
Also, when the “Use/Equip” button is clicked, it says a boolean variable “use” to true.
Here is my code: (It is in the update function and will be called every frame)
First thought as I read your post/code:
No need for that in the Update function. I would move it. This is based on performance & usefulness… not in actually fixing the other problem.
Well, I love problem solving even in code that should be changed… however, I can’t find it right this second & have to go out for a bit.
So, what I would suggest is that you make methods that are called when you’re: adding , using, deleting, etc…
Inside those methods, adjust the inventory items and their text values, as appropriate.
This will be much better all around.
Hey, I’m back but since I really think the code should move… maybe once you set it up to be “when it happens” rather than constantly looping, you could post fresh code for that.
Oh, you responded as I was writing. I thought about that (finding the problem… still curious)…but the structure will be different when you do it based on an action (re: “when it happens”/event).
So, I believe in doing that, you’ll also be fixing it… that’s not exactly what I wanted to say, but close enough
Did you copy/paste this code? I asked because this variable is named differently: itemSlotTextAmount from line #14 in your post.
In response to “What do you mean by that?” I meant that if you change your code so when you select an item, and click “use” , you run a method that then uses the item, and updates its quantity & the text displayed. It runs at that time, because you clicked use - that’s it… not in Update().
Loool, I think you helped me figure out why it wasnt working. Like you said, line14 in my code is named differently, “item” should have been spelt with a capital “I”
Yeah I will. I’m thinking I should make a method that sets the “text_ItemAmount” of the item slot to the correct quantity, and call that in the update function so whenever the quantity of an item changes, it will automatically update it, and I will make a seperate method for using items so it isn’t constantly being called in the update function like you said
No! lol…I mean… Okay, you admitted/agreed to part of it…
but think about your situation, whenever an item is “used”… you would know about it!
You don’t need to scan it a bazillion times to know the amount has changed.
It changes when you : use it, drop it, sell it, whatever…
Hmm yeah you are right, that is much more efficient. I didn’t realise that, I was too focused on fixing the problem lol. Thanks so much! Is it alright if I post the new code after do what you suggested so you can see if I made a mistake?
Sure… First, you could probably just run it to see if there is a mistake?
But of course you can post some nice working code (or even if it’s broken)… if you want
Yes, I think it will be more efficient. Plus, you’ll be acting on only the selected (or changed) object with the new design. You won’t have to go over the whole list (except for maybe when you add to the list, to find the item or free space for it…)