Coin Shop

Bonus Part: Creating a Shop to Spend Your Collected Coins

14 min read 19K+ views Oct 16, 2021
Part 5 of 5 - Visual Scripting Series (Bonus)

This final bonus tutorial shows you how to create a coin shop where players can purchase different player skins using the coins they've collected.

1 Creating the Shop Scene

  1. Duplicate the Start scene (Ctrl+D)
  2. Rename it to "Coin Shop"
  3. Change the title text to "Shop"
  4. Anchor the title to the top center

2 Creating the Item List

  1. Create an empty GameObject called "List"
  2. Add Horizontal Layout Group component
  3. Set Spacing to 20
  4. Set Child Alignment to Upper Center
  5. Stretch the RectTransform to fill the width
  6. Uncheck Child Force Expand (Width)
Layout Groups

Unity provides Horizontal, Vertical, and Grid Layout Groups for automatic UI arrangement. Choose based on your shop design.

3 Creating Shop Items

Item Structure

  1. Add UI → Image inside the List
  2. Set a color (e.g., Blue) - this represents the skin
  3. Name it "Blue"
  4. Copy the coin UI from your level and paste it as a child
  5. Anchor to bottom-right, scale to 0.7
  6. Change the price text to "10"
  7. Remove the Script Machine from the price text (we'll handle it differently)

Item Variables

Add a Script Machine to the item with these Object Variables:

  • ID (String): "blue" - unique identifier for saving
  • Color (Color): The actual color value
  • Price (Integer): 10
  • Price Text (GameObject): Reference to the text object

4 Item Configuration Graph

Create a graph called "Item Graph" with On Start logic:

Setting the Color

  1. Add Image Set Color
  2. Connect your Color variable

Setting the Price Text

  1. Add Set Text
  2. Convert Price (Integer) to String
  3. Use Price Text variable as the target

5 Creating Multiple Items

  1. Duplicate the Blue item
  2. Create Orange (price 20), Green (price 25), Red (price 0)
  3. Update each item's ID, Color, and Price variables
  4. Red is the default free skin

6 Player Skin Variable

  1. Go to Saved Variables
  2. Add "Player Skin" (Color type)
  3. Set default to Red

Applying Skin to Player

  1. Open the Player prefab graph
  2. On Start: Get the Player Skin saved variable
  3. Add Mesh Renderer Get Material
  4. Add Material Set Color
  5. Connect the saved color

7 Purchase Logic

Add On Pointer Click event to the item graph:

Check if Already Purchased

  1. Add Get Saved Variable (using ID as the name)
  2. Set Fallback to False (for items not yet purchased)
  3. Add If unit

If Already Purchased (True)

  1. Just select the skin (set Player Skin variable)
  2. Load the first level (build index 1)

If Not Purchased (False)

  1. Check if coins >= price
  2. If yes:
    • Subtract price from Coins
    • Save the purchase (set ID variable to True)
    • Set Player Skin to this color
    • Load the first level
  3. If no: Do nothing (or show an error)
Using Fallback

Always use the Fallback option when getting saved variables that might not exist yet. This prevents errors when checking for unpurchased items.

8 Hiding Price for Owned Items

In the configuration (On Start), after checking if purchased:

  1. If the item is already purchased:
  2. Get the Price Text's parent (the coin icon group)
  3. Set Active to False

9 Setting Default Owned Items

  1. Go to Saved Variables
  2. Add "red" (Boolean) = True
  3. This marks the red skin as already owned

Complete Shop Flow

  1. Player opens shop
  2. Owned items show without price
  3. Click an owned item → Select skin and start game
  4. Click an unowned item:
    • Have enough coins → Purchase, select, and start game
    • Not enough coins → Nothing happens

Extending the Shop

Ideas for further development:

  • Show an error message when coins are insufficient
  • Highlight the currently selected skin
  • Add a "Back" button to return to the main menu
  • Display current coin count in the shop
  • Add more customization options (meshes, particles, etc.)

Series Complete!

Congratulations on completing the Visual Scripting series! You've learned to build a complete game with multiple levels, UI, animations, and a coin shop - all without writing code.