Unity Essentials: Physics, UI & Gameplay Systems

Lesson 5 of 8

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 new EnemyData assets (Right-click → Create → Game → Enemy Data) without touching code.
  • Every Enemy prefab that shares the same EnemyData asset 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 MonoBehaviour would 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).

📝 ScriptableObjects Quiz

Passing score: 70%
  1. 1.Where does a ScriptableObject asset live?

  2. 2.The [____] attribute adds a menu entry so designers can create new instances of a ScriptableObject without code.

  3. 3.Multiple prefabs can reference the same ScriptableObject asset and share its data.