Menu
CoddyTech

Shop and Inventory

Part of the Building Real Games section of Coddy's Unity journey. Lesson 57 of 60.

Between levels, the hero visits a shop. Three items wait on the shelf, a Helmet, Boots and a Cape, each on a button showing its price, and a Work button earns coins for the ones the hero cannot afford yet. What the hero buys goes into the inventory listed at the bottom, and the coins left and the items owned are still there the next time the game starts.

Write two scripts: Shop on the Shop object, and ShopItem on each item's button.

challenge icon

Challenge

Hard

Write Shop and ShopItem, saved with PlayerPrefs.

  1. Shop: coins under Coins (new player: startCoins), shown as Coins: 100; Work adds pay.
  2. ShopItem: owned when Owned_Helmet is 1; label Helmet: 40 or Helmet: owned; clickable while not owned and affordable; a click buys.
  3. inventoryText: Inventory: Helmet, Cape or Inventory: empty. Save and redraw on every change.

Press Play, shop, Stop and play again. Then Check.

Try it yourself

using UnityEngine;
using UnityEngine.UI;
using TMPro;

public class Shop : MonoBehaviour
{
    public int startCoins = 100;
    public int pay = 25;
    public TMP_Text coinsText;
    public TMP_Text inventoryText;
    public Button workButton;
    public ShopItem[] items;
}

All lessons in Building Real Games

Practice on your own: Unity playground