1
\$\begingroup\$

I am trying to make an inventory system in Unity with ScriptableObjects and a text driver that displays the contents of an inventory. So far the code looks like this:

-An InventoryObject is defined as having a "Container" List of ItemObjects (simple scriptable objects, they just have a little information on the object as its sprite, its name, and whether or not it is stackable) and some methods to properly add or remove items (taking item stacking into account) from that list. https://pastebin.com/t4P3aGmi

-A PlayerInventory is a script that will be carried by the player's character. It contains a method for adding items collided with to its assigned InventoryObject instance. It also contains an OnItemPickedUp event that takes its InventoryObject instance as a parameter and passes it to listeners like UpdateDisplay. OnItemPickedUp is raised whenever a collision with a compatible item happens. https://pastebin.com/RsV7XYPp

-An UpdateDisplay script that listens to the OnItemPickedUp event and subscribes the ProcessInventoryToSingleString(InventoryObject _inventory) method, that subsequently calls several methods to transform the contents of the InventoryObject's Container list into a single, formatted string. https://pastebin.com/N0nsvcWH

-The items are represented by giving a GameObject with a collider2D, aSpriteRenderer and script that contains the particular ItemObject's data and sets its SpriteRenderer's sprite to the ItemObject's.

The problem I'm having is that the method subscribed to the event is being called more than once for each time the event is raised. as shown by the debug logs. Sometimes it's called twice, sometimes even four. This results in my UI display showing more items than there are. https://files.catbox.moe/afa467.mp4

https://files.catbox.moe/6oxh9b.mp4

Here are pictures of the console debug logs: I'd copypaste the debug logs, but I can't do that in one go.

I appreciate any help.

EDIT: These errors also appear. I'd like to believe that they're unrelated. enter image description here

\$\endgroup\$
4
  • \$\begingroup\$ From your log, it does not look like the subscribers of the event are being called twice per invocation, but rather that the OnTriggerEnter is being called twice. Are you possibly entering overlap with multiple colliders at once? Or do you have two PlayerInventory instances that are both processing the collision? Also, do you get any errors not shown here? I'm having trouble making sense of this sequence of logs unless one of the two OnTriggerEnter2D calls threw an exception before getting to the "Event Raised" log line. \$\endgroup\$
    – DMGregory
    Apr 6 at 21:12
  • \$\begingroup\$ Interesting that the collider might be the thing giving trouble, it does not do the debug message "Player-Item collision!" that it's supposed to when colliding.. Why is it colliding with something that has been Destroyed()? As for overlapping colliders, none at all. No double PlayerInventory, either. I do get some completely unrelated (i hope) errors shown (nullreference for some spriterenderer) \$\endgroup\$ Apr 6 at 21:27
  • \$\begingroup\$ Never omit errors from a question here. They are always relevant. If you're sure they're unrelated, make a new scene that demonstrates your problem with no console errors first, and show us the log from that. \$\endgroup\$
    – DMGregory
    Apr 6 at 21:30
  • \$\begingroup\$ Very well, i'll add them to the bottom of the question. \$\endgroup\$ Apr 7 at 7:55
0
\$\begingroup\$

I fixed it. There was a duplicate script subscribing twice to the events, but deleting it didn't fix the problem. The problem was in InventoryObject's AddItem() function. I forgot to add a Break; statement to the first Else, and that screwed everything up. Here's what it should look like. enter image description here

\$\endgroup\$
1
  • \$\begingroup\$ Code should be shared as correctly formatted text, not images or external links. If those pastebin links rot, then it's unlikely a future reader will be able to learn anything useful from this tiny snippet. So be sure to show enough context that the problem and solution are clear when reading this page alone. \$\endgroup\$
    – DMGregory
    Apr 7 at 14:45

Not the answer you're looking for? Browse other questions tagged or ask your own question.