Robust Unity Currency System with Scriptable Objects
Let’s face it: many in-game economies feel as brittle as Monopoly money. Implementing a solid currency system in Unity, especially for beginners, often feels like a daunting task. But it doesn’t have to be. We’re going to bypass the typical pitfalls and create a robust, scalable, and easily understandable currency system using Scriptable Objects and clear, concise code. Forget complex tutorials with dozens of scripts. We’re focusing on what actually matters: a clean, functional system you can build upon.
Setting Up Your Currency with Scriptable Objects
The most-downloaded assets on Wayline this month — sorted by what real developers actually use.
Why Scriptable Objects? Because they are data containers independent of scene objects, meaning you can easily access and modify currency data without tying it to a specific GameObject instance. This promotes reusability and avoids data duplication. This is crucial for maintainability, especially as your game grows.
Create the Scriptable Object Script:
Create a new C# script named
Currency.csand paste the following code:using UnityEngine; [CreateAssetMenu(fileName = "NewCurrency", menuName = "Currency")] public class Currency : ScriptableObject { public string currencyName; public string currencySymbol; public int startingAmount; [HideInInspector] public int currentAmount; public void OnEnable() { currentAmount = startingAmount; } }This script defines the basic properties of your currency: its name, symbol, and initial amount. We use
HideInInspectorto prevent thecurrentAmountfrom being directly modified in the Inspector, encouraging the use of code-based modifications. TheOnEnablemethod ensures thecurrentAmountis reset to thestartingAmountwhen the game starts or when the Scriptable Object is reloaded, preventing lingering data from previous play sessions.Create the Scriptable Object Instance:
In your Project window, right-click, go to
Create->Currency. Name it something descriptive like "Gold". Now, in the Inspector, you can set the name (e.g., “Gold”), symbol (e.g., “G”), and starting amount (e.g., 100). You’ve just defined your game’s core economic unit.
Displaying Currency in the UI
Now, let’s show that hard-earned gold to the player. We’ll use a simple Text component in a Canvas.
Create a UI Text Element:
In your scene, create a Canvas (if you don’t have one already) and add a Text component as a child. Position and style it to your liking.
Create the Currency Display Script:
Create a new C# script named
CurrencyDisplay.csand attach it to a GameObject in your scene (typically the Canvas itself, or a dedicated UI manager).using UnityEngine; using UnityEngine.UI; public class CurrencyDisplay : MonoBehaviour { public Currency currency; public Text currencyText; void Update() { currencyText.text = currency.currencySymbol + currency.currentAmount.ToString(); } }Drag your Currency Scriptable Object instance to the
currencyfield in the Inspector. Also, drag the Text component to thecurrencyTextfield. This script updates the UI Text every frame with the current currency amount and its symbol.
Pitfalls: A common mistake is forgetting to assign the Scriptable Object or the Text component in the Inspector. Double-check those connections! Another pitfall is using FixedUpdate instead of Update. Currency amounts are better updated on every frame, rather than fixed physics updates, to avoid UI lag.
Adding and Subtracting Currency
This is where the rubber meets the road. Let’s create methods to modify the currency amount.
Extend the Currency Script:
Add these methods to your
Currency.csscript:public void AddCurrency(int amount) { currentAmount += amount; } public void SubtractCurrency(int amount) { if (currentAmount >= amount) { currentAmount -= amount; } else { Debug.LogWarning("Insufficient currency!"); } }These methods allow you to add and subtract currency, with a check to prevent the player from going into negative currency. A warning message is logged when the player tries to spend more than they have.
Example Usage:
Let’s say you have a shop item that costs 50 gold. You would call
currency.SubtractCurrency(50)when the player buys the item. Similarly, when a player completes a quest that rewards 100 gold, you’d callcurrency.AddCurrency(100).Here’s an example script to trigger currency changes on button clicks (create a Button in your UI and attach this script to it):
using UnityEngine; using UnityEngine.UI; public class CurrencyButton : MonoBehaviour { public Currency currency; public int amountToAdd; public int amountToSubtract; public void AddCurrency() { currency.AddCurrency(amountToAdd); } public void SubtractCurrency() { currency.SubtractCurrency(amountToSubtract); } }In the Inspector, connect your Currency Scriptable Object and set the amounts to add or subtract. Then, link the
AddCurrencyandSubtractCurrencymethods to the button’sOnClickevent.
The Scalability Advantage: Multiple Currencies
The beauty of this system is its easy scalability. Want to add a second currency, like "Gems"? Simply create another Currency Scriptable Object instance, set its name, symbol, and starting amount, and create another UI Text element to display it. You can then create separate scripts or extend existing ones to handle Gems independently.
Overcoming the Challenges
A common challenge is managing multiple currencies and their exchange rates. Implementing a system to track these rates within Scriptable Objects or dedicated manager classes is crucial for complex in-game economies. Another pitfall is neglecting data persistence. Saving the currentAmount to PlayerPrefs or a more robust save system ensures that the player’s currency is not lost when they close the game.
This simple yet powerful currency system is a fantastic starting point for any Unity game. It is easy to understand, implement, and extend, providing a solid foundation for building complex in-game economies. By leveraging Scriptable Objects and clear code, you can avoid common pitfalls and create a game economy that is both engaging and sustainable.