Journal 107 — Unity 2D Mobile, Dungeon Escape Shop Setup Part 3

Chris Nielsen
3 min readOct 15, 2021

Objective: To implement purchasing items in the shop

In the previous two articles, we have set up the UI of the shop. In this article, we will finalize the functionality so items can be purchased.

We can start with updates to the shop C# script to create a buy item method. We can expand the switch statement to identify which item is selected in each statement along with its price. From there, we can check if the player has enough diamonds to purchase the item. Otherwise the shop screen closes.

Next, we can add an onclick event to the buy button. Then we can try and see if the shop closes due to insufficient amount of gems.

Now we can test if a purchase works. We can add some gems to the player via the inspector, and also add a couple debug.log statements.

Here we can test this in action.

Add Game Manager

The final part of the shop buying system is to add a Game Manager to control adding the power ups, to give a key to win the level, and to control watching ad videos to get more diamonds.

We can make a GameManager C# script and turn it into a singleton. We can include a public bool variable to set if the player purchases the key to the castle.

Now we can see this in action.

Paint Gems

We can add several diamonds to the level to give the player gems to purchase the power ups. To do this, we need to use a diamond prefab we created previously, and we can use the Tile Palette to create a GameObject Brush.

In Unity 2021, we need to start with one diamond prefab already placed in the Scene. From there, use the Tile Palette window and switch the brush from “Default” to “GameObject Brush”, and drag in the diamond game object. Also make sure the prefab is on Order in Layer 50 so they are visible in all locations. To keep things in order, you can also make a 2D tilemap to hold the diamonds on Layer 50.

To add these diamonds, switch to the paint brush using the GameObject Brush and place several diamonds in the level.

Now we can see if collecting diamonds works.

Thank you for your time!

--

--

Chris Nielsen

An Engineering Manager consultant who is seeking additional skills using Unity 3D for game and application development.