In this lesson, you will learn how to build modular ScriptableObject templates that serve as centralized "control panels" for your game's balance-critical stats.
Centralizing the Balance
When you hard-code values like health = 100 or speed = 5 directly into your MonoBehaviour scripts, you force yourself to open every individual prefab or script just to tweak the game’s feel. By creating a modular data container, you move these variables into a standalone asset file that acts as a single source of truth.
To start, define a "Data Template" script. This class shouldn't contain logic; it should only hold the data variables you want to balance:
using UnityEngine;
[CreateAssetMenu(fileName = "NewEnemyStats", menuName = "Game/Enemy Stats")]
public class EnemyStats : ScriptableObject
{
public float maxHealth;
public float movementSpeed;
public float damageOutput;
}
Decoupling Data from Prefabs
Once you create this asset in your Project window (by right-clicking -> Create -> Game -> Enemy Stats), you can assign it to any enemy prefab via a reference field in your main script:
public class EnemyController : MonoBehaviour
{
public EnemyStats stats; // Reference the data container here
void Start()
{
float health = stats.maxHealth;
// Logic uses the values from the container
}
}
By structuring your project this way, you can create multiple variations of the same enemy—like a "Fast Scout" and a "Tanky Juggernaut"—simply by creating two different EnemyStats assets. Because these are files in your Project, you can drag and drop them into your prefabs or even swap them at runtime to change behavior dynamically.
This approach allows you to adjust the "feel" of the entire game while in Play Mode. If you change a value in the ScriptableObject asset while the game is running, the reference updates instantly across all prefabs using that data. You are no longer digging through object hierarchies to tune values; you are treating your balance settings as a library of assets.
Key Takeaway: By offloading stats into modular ScriptableObject assets, you decouple data from logic, enabling rapid iteration and a centralized hub for all your balancing adjustments.