Introduction
In almost every successful game, players expect to earn, spend, and manage currency. Whether it’s gold coins in an RPG, gems in a puzzle game, or upgrade tokens in a shooter — having a clean currency and shop system is essential.
In this Unity tutorial, you’ll learn how to create a fully functional in-game currency system and a basic shop UI with Buy and Cancel functionality using Unity UI and C# scripting. Perfect for both mobile and desktop games.
Step 1: Set Up the UI Elements
Before writing code, let’s lay out a simple UI:
- Coin Display (
Text
orTMP_Text
) — e.g., “Coins: 100” - Buy Button — Executes the purchase
- Cancel Button — Exits the transaction
- Item Display Panel — Represents the purchasable item (optional image/icon)
Tip: Group all UI elements inside a Canvas. Use a Vertical Layout Group to stack shop entries neatly.
Step 2: Currency Manager Script
This script will control the coin balance and allow other scripts to access it.
using UnityEngine;
using UnityEngine.UI;
public class CurrencyManager : MonoBehaviour
{
public static CurrencyManager Instance;
public int currentCoins = 100;
public Text coinText;
void Awake()
{
if (Instance == null)
Instance = this;
}
void Start()
{
UpdateCoinUI();
}
public bool SpendCoins(int amount)
{
if (currentCoins >= amount)
{
currentCoins -= amount;
UpdateCoinUI();
return true;
}
return false;
}
public void AddCoins(int amount)
{
currentCoins += amount;
UpdateCoinUI();
}
void UpdateCoinUI()
{
coinText.text = "Coins: " + currentCoins.ToString();
}
}
✅ Use a singleton pattern for easy global access throughout your game.
Step 3: Shop Item Script
This script goes on each item in your shop. It handles buying logic and interacts with the currency system.
using UnityEngine;
using UnityEngine.UI;
public class ShopItem : MonoBehaviour
{
public int cost = 50;
public Button buyButton;
public Button cancelButton;
public GameObject itemToUnlock;
void Start()
{
buyButton.onClick.AddListener(BuyItem);
cancelButton.onClick.AddListener(CancelPurchase);
}
void BuyItem()
{
if (CurrencyManager.Instance.SpendCoins(cost))
{
Debug.Log("Item purchased!");
itemToUnlock.SetActive(true);
}
else
{
Debug.Log("Not enough coins!");
}
}
void CancelPurchase()
{
Debug.Log("Purchase cancelled.");
}
}
️ You can easily expand this later to include sound, visual feedback, or confirmation popups.
Step 4: Testing Coin Earning
Want to simulate coin collection during testing? Add this simple input check:
void Update()
{
if (Input.GetKeyDown(KeyCode.C))
{
CurrencyManager.Instance.AddCoins(10);
}
}
Press the C key during Play mode to earn 10 coins each time.
✅ Pro Tips for a Better Shop System
1. Use ScriptableObjects for Item Data
Store item name, cost, icon, and effects in a ScriptableObject. This keeps things modular.
2. Save Currency with PlayerPrefs
PlayerPrefs.SetInt("Coins", currentCoins);
3. Use Sound & Visual Feedback
Play a sound or animate the button when a purchase succeeds or fails.
4. Add Item Descriptions
Display hover or tap info for what each item does.
❌ Common Mistakes to Avoid
- Having duplicate buttons labeled “BUY” (use “CANCEL” clearly)
- Forgetting to update the coin text when values change
- Spawning unlocked items multiple times without checks
- Not disabling buttons after purchase
Bonus Ideas for Future Expansion
- Add a daily coin reward system
- Implement rewarded video ads to earn coins
- Create premium and free shops
- Add localization support for shop text
Final Thoughts
Unity’s UI system and C# scripting make it incredibly easy to create a robust and flexible in-game shop. Whether you’re offering skins, boosts, or unlockables, this simple structure can scale to meet almost any project need.
Build this once — and you’ll reuse it across multiple games!
Do your players love earning coins — or are they spending too fast? Build the system that keeps them coming back!