Problems with methods being called

Hi all, I am somewhat of a rookie at coding, and I am following the lecture series on Udemy on making a Stardew Valley style farming game, and hit a roadblock (also not getting a response on this one from the lecturer.)

I have a problem where when the PC picks up an item, it adds two of the item to the inventory. I have followed the lecture scripts to the letter (I think!) and have spent the whole day looking for an error that I just cannot find.

the problem is that there are two AddItemAtLocation methods (one overloaded), both called from the AddItem method, and I suspect they are both being called each time an item is picked up, rather than just one of them, but I cannot figure out why.

public void AddItem(InventoryLocation inventoryLocation, int itemCode)
{
List inventoryList = inventoryLists[(int)inventoryLocation];
// Check if inventory already contains the item
int itemPosition = FindItemInInventory(inventoryLocation, itemCode);
if (itemPosition != -1)
{
AddItemAtPosition(inventoryList, itemCode, itemPosition);
}
else
{
AddItemAtPosition(inventoryList, itemCode);
}
// Send event that inventory has been updated
EventHandler.CallInventoryUpdatedEvent(inventoryLocation, inventoryLists[(int)inventoryLocation]);
}

here is what happens in the console when I pick up an item:

Item Description:Acorn Item Quantity: 2
UnityEngine.Debug:Log (object)
InventoryManager:smile:ebugPrintInventoryList (System.Collections.Generic.List1<InventoryItem>) (at Assets/Scripts/Inventory/InventoryManager.cs:105) InventoryManager:AddItemAtPosition (System.Collections.Generic.List1,int,int) (at Assets/Scripts/Inventory/InventoryManager.cs:75)
InventoryManager:AddItem (InventoryLocation,Item) (at Assets/Scripts/Inventory/InventoryManager.cs:48)
InventoryManager:AddItem (InventoryLocation,Item,UnityEngine.GameObject) (at Assets/Scripts/Inventory/InventoryManager.cs:37)
ItemPickUp:OnTriggerEnter2D (UnityEngine.Collider2D) (at Assets/Scripts/Player/ItemPickUp.cs:18)

please can someone suggest a solution?

When you have two functions differing only by one extra argument, a common pattern is for the one without the argument to just call the one WITH the argument, and provide an arbitrary additional argument.

This lets you avoid duplicating any code. Duplicating code is a bad thing.

Is that what is going on?

Here’s my general how-to-track-down-further blurb:

What is often happening in these cases is one of the following:

  • the code you think is executing is not actually executing at all
  • the code is executing far LESS OFTEN than you think
  • the code is executing far MORE OFTEN than you think
  • the code is executing on another GameObject than you think it is

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run? what order does it run in?
  • what are the values of the variables involved? Are they initialized? Are the values reasonable?
  • are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

Knowing this information will help you reason about the behavior you are seeing.

You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

Thanks Kurt! I will try that. I’m peppering the script with debug.log statements to see exactly what is going on.

So by adding debug.logs I could see that BOTH methods were being triggered. The problem I have is I am following a lecture series, and the tutors script worked flawlessly. As far as I can see I have copied it to the letter, but my results are just broken :frowning: