ScriptableObjects: Data-Driven Design
A ScriptableObject is a data container that lives as an asset in your project, not attached to any GameObject, perfect for shared configuration that many objects reference.
using UnityEngine;
[CreateAssetMenu(fileName = "New Enemy", menuName = "Game/Enemy Data")]
public class EnemyData : ScriptableObject
{
public string enemyName;
public int maxHealth;
public float moveSpeed;
public GameObject prefab;
}
public class Enemy : MonoBehaviour
{
public EnemyData data;
int health;
void Start()
{
health = data.maxHealth;
}
}
Why not just fields on the MonoBehaviour?
[CreateAssetMenu]adds a menu entry so designers can create newEnemyDataassets (Right-click → Create → Game → Enemy Data) without touching code.- Every
Enemyprefab that shares the sameEnemyDataasset reads the same values, tweak the asset once, every enemy using it updates, no need to edit each prefab individually. - ScriptableObjects aren't part of the scene, so they don't get destroyed when a scene unloads, and they don't duplicate per-instance memory the way fields on a
MonoBehaviourwould across hundreds of enemies.
Common uses
Item databases, enemy/weapon stats, dialogue data, and even shared event channels (a ScriptableObject-based alternative to C# events that decouples systems that don't reference each other directly).