Note Pickup System for 2D game

I want to make a system in my game where whenever you walk up to a note and can press E to read it, then once you close the menu, the note disappears, goes into your inventory and you can re-read it from there? I don’t have an inventory made yet and I don’t want to make on without this code, as I don’t want to mess anything up.

It sounds like you should first get familiar with Unity API features, and then try to string it together, I recommend going through some Unity Learn tutorials before trying this. But here is how I would do it.

You can detect if player is close to a note, by either having a OnTriggerEnter2D or OnTriggerStay2D method in either player or in note, so for example, note can now detect that player is close.

Store the state to boolean like playerIsClose, and only then check in the Update method if player pressed some key like E, using Input.GetKeyDown for example.

When player presses key, you check if playerIsClose, and then you will have note to call / message some of your game’s method, that will receive the contents of the note and it will also open a UI canvas / panel, which shows the message text on some UI text element.

When the player does something to close the note (presses a key, which you can check with Input.GetKeyDown again), call some method on your inventory, which will add a given object to inventory. This could be the same method you use to add any other picked up items from ground. Now this could be simply by giving it some id, or a C# class object reference or a struct, but usually not an actual scene gameobject.

At this point, you can also destroy the in-game representation of your note i.e. the gameObject (which had your note component) using Destroy. Usually gameObjects are not something you want to store into your inventory, you are usually only interested in knowing what you have in your inventory (or shop, or loot box), especially if you are interested in saving your game state and your inventory data along with it.

Reading the note from inventory is again a very similar setup, you’ll have to have a way to select items from inventory and most likely visually mark that something is selected, then when the player has selected some item from inventory, you’ll have to check for player input, and if the target slot happens to have a note item type, then you’ll call the same ShowNote method like you did earlier, but most likely you probably want to have some more generic method, like Use, which each item type has.