Day 26: Creating a shop system

Objective: To create a shop system that will allow players to purchase in game items and add those purchases to their inventory.
We have a simple foundation for a combat system in place for this 2.5d adventure platformer project. Now we can create the base for a shop and inventory system. Shops and player inventories are staple features when it comes to adventure and RPG games. We will build a simple stripped down version of an inventory/shop system.
First we will use another NPC character to represent an in game merchant NPC that our player will have to interact with to bring up the shop menu.

As we know by now this object needs a trigger zone and a rigid body component for the Player to interact with it. We also need a script for the shop keeper to interact with the player and update all of the necessary UI elements.

We need to create a shop panel to represent the shop.
I wont go into how to create this simple UI as there are several tutorials and videos on how to wire up Unity’s UI.

Our shop script will need the variables seen in the image above. For now we will activate and deactivate the shop window with OnTriggerenter and OnTriggerExit.

This script will need a function to update the player and UI manager scripts when we select and purchase an item. This switch statement will update the needed variables.

When we have our item selected we will call the BuyItem() Function.

We now need to go into our UI manager to create a function that Updates the selection cursor’s Y position.

With these systems wired up we now need to have our player create a SpendCoin() function that will subtract from the players coin count.

In the next segment we will add the item into the inventory that we havent set up yet. The scaffolding for this system can be seen in action in the gif at the top of this article.