←All patterns
Game Programming

ScriptableObject Events

Turn events into ScriptableObject assets, so scene objects and prefabs talk through a shared file wired in the Inspector instead of through each other.

What Is It?

A ScriptableObject event channel is an asset in your Project, such as OnPlayerDied.asset. The object that raises the event has a field pointing at the asset and calls Raise(). Listeners point at the same asset and register with it. Raiser and listeners never reference each other; they only reference the file.

The idea was popularised by Ryan Hipple's Unite Austin 2017 talk “Game Architecture with Scriptable Objects”. Its core pieces are a GameEventSO that keeps a list of listeners, and a generic GameEventListener MonoBehaviour whose UnityEvent response is wired in the Inspector. A typed variant such as IntEventChannelSO exposes event Action<int> for code-side listeners that need a payload.

Because assets exist outside any scene, the channel works across additive scenes, and a prefab can reference it directly, something it can never do with a scene object. That is the difference from Observer (you need the subject's reference) and from a static Event Bus (the wiring lives in code, invisible in the Inspector). Here the connection is a visible, draggable asset reference.

When Is It Used?

Use it when designers should be able to wire reactions without code: “when OnPlayerDied is raised, play this animation, enable that panel”.

It shines in projects split into several additive scenes (gameplay, UI, audio) and in prefab-heavy projects, where a direct reference between the two sides is impossible or fragile.

Avoid it for very high-frequency events and for tightly coupled local logic. One asset per event can become hundreds of small files, and searching the Project for “who listens to this?” is slower than reading code.

Interactive Demo

Click the controls and watch the objects collaborate. The console mirrors what the C# code below would log.

ScriptableObject Events Pattern
HP100listeners1raised0cross-scene refs0
▤ Project
Gameplay.unity
UI.unity additive
◎OnPlayerDied
GameEventSO
◎HealthChanged
IntEventChannelSO
◆Enemy
Prefab
Player
OnPlayerDied
HealthChanged
GameOverPanel
OnPlayerDied
Response → SetActive(true)
HealthBar
HealthChanged
Console
Press a button to see what happens…

Code

Assets / Scripts / Game Programming/ ScriptableObjectEvents ›PlayerHealth.cs
using UnityEngine;

namespace Patterns.GameProgramming.SOEvents
{
    /// <summary>
    /// The raiser. Its only links to the outside world are two channel assets
    /// dragged in from the Project window; it never finds or references a listener.
    /// </summary>
    public class PlayerHealth : MonoBehaviour
    {
        [SerializeField] private int maxHealth = 100;

        [Header("Event channels (assets)")]
        [SerializeField] private IntEventChannelSO healthChanged; // HealthChanged.asset
        [SerializeField] private GameEventSO playerDied;          // OnPlayerDied.asset

        private int current;

        private void Awake() => current = maxHealth;

        public void TakeDamage(int amount)
        {
            if (current <= 0) return;

            current = Mathf.Max(0, current - amount);
            healthChanged.Raise(current);

            if (current == 0)
                playerDied.Raise();
        }
    }
}
4 files · namespace Patterns.Game Programming.ScriptableObjectEventsC# · UTF-8 · LF

Before & After

Real-world smell: a game-over screen wired through a static class and a scene search. Here is the code without the pattern, the refactored version, and the exact diff between them.

! What’s wrong with it?

GameOverScreen subscribes to the static GameEvents.PlayerDied event and calls FindAnyObjectByType<ScoreKeeper>() in Awake to read the final score.

When the UI moves to its own additive scene, the search runs before the gameplay scene is loaded and returns null. Nothing in the Inspector shows that the screen depends on GameEvents or ScoreKeeper, and every script in the project can raise GameEvents.RaisePlayerDied().

✓ What changed

  • The scoreKeeper field, the FindAnyObjectByType call and the static GameEvents class are gone.
  • A serialized IntEventChannelSO playerDied field references a channel asset, wired in the Inspector and visible to designers.
  • The final score arrives as the event payload in Show(int finalScore), so the screen no longer reaches into another scene for data.
  • Subscription still pairs += in OnEnable with -= in OnDisable, now on the asset's OnRaised event.
GameOverScreen.Before.cs → GameOverScreen.After.cs+8/−23
1−using System;
21 using TMPro;
32 using UnityEngine;
43
54 namespace Patterns.GameProgramming.SOEvents
65 {
76 /// <summary>
8− /// Without event channels: the screen listens to a global static class
9− /// and searches the scene for the object that owns the score.
7+ /// With event channels: the screen references an asset, not a scene object.
8+ /// The final score arrives as the event's payload.
109 /// </summary>
1110 public class GameOverScreen : MonoBehaviour
1211 {
1312 [SerializeField] private GameObject panel;
1413 [SerializeField] private TMP_Text finalScoreLabel;
1514
16− private ScoreKeeper scoreKeeper;
15+ // Drag the PlayerDiedScore.asset channel here. Payload: the final score.
16+ [SerializeField] private IntEventChannelSO playerDied;
1717
18− private void Awake()
19− {
20− // Returns null when the UI lives in an additive "UI" scene
21− // that is loaded before the gameplay scene.
22− scoreKeeper = FindAnyObjectByType<ScoreKeeper>();
23− }
18+ private void OnEnable() => playerDied.OnRaised += Show;
2419
25− private void OnEnable() => GameEvents.PlayerDied += Show;
20+ private void OnDisable() => playerDied.OnRaised -= Show;
2621
27− private void OnDisable() => GameEvents.PlayerDied -= Show;
28−
29− private void Show()
22+ private void Show(int finalScore)
3023 {
31− finalScoreLabel.text = scoreKeeper.Score.ToString();
24+ finalScoreLabel.text = finalScore.ToString();
3225 panel.SetActive(true);
3326 }
3427 }
35−
36− /// <summary>The project-wide static hub every script reaches into.</summary>
37− public static class GameEvents
38− {
39− public static event Action PlayerDied;
40−
41− public static void RaisePlayerDied() => PlayerDied?.Invoke();
42− }
4328 }

Advantages & Disadvantages

+ Advantages

  • Wiring is visible and editable in the Inspector; designers can add reactions with GameEventListener and a UnityEvent.
  • Works across scenes and from prefabs, because the asset is not owned by any scene.
  • Easy to test: drop a listener in an empty scene and raise the asset from a button or a custom Inspector.

− Disadvantages

  • Many small assets to name, organise and find; the Project window becomes part of your architecture.
  • Assets are shared and persistent, which is easy to misunderstand: in the Editor, serialized values changed during Play Mode stay changed on the asset after you stop.
  • Flow is spread between code and serialized data, so “who reacts to this?” needs Find References In Scene or a custom tool.

Tips

  1. 01Register in OnEnable and unregister in OnDisable, exactly like C# events, so disabled and unloaded objects drop off the list.
  2. 02Add a custom Inspector with a Raise button to the channel asset; it makes testing a scene in isolation trivial.
  3. 03Use a generic base like EventChannelSO<T> and tiny subclasses (IntEventChannelSO, VoidEventChannelSO) because Unity cannot serialize open generic asset types.
  4. 04Keep a description field on each asset explaining who raises it, and group channels in one folder per feature.
  5. 05Unity's own Open Projects sample “Chop Chop” uses this style throughout; it is a good reference for structuring channels at scale.