In this lesson, you will learn how to create a reusable ScriptableObject that acts as a central communication hub, allowing disparate game systems to talk to each other without being directly connected.
The Problem of Tight Coupling
In many projects, you might find your Player script directly referencing your UIManager script. This is "tight coupling." If you delete the UI, your Player script breaks. We want to decouple these systems so they can interact without knowing the other exists.
The Event Channel Pattern
A ScriptableObject Event Channel acts as a "radio station." The sender "broadcasts" a message on the channel, and any number of listeners can "tune in" to receive that message. Because the channel is a standalone asset, the sender and listener never need to hold references to one another.
Creating the Template
First, define your channel as a generic template. Here is a simple example for a parameter-less event:
using UnityEngine;
using UnityEngine.Events;
[CreateAssetMenu(menuName = "Events/Void Event Channel")]
public class VoidEventChannelSO : ScriptableObject
{
public UnityAction OnEventRaised;
public void RaiseEvent()
{
OnEventRaised?.Invoke();
}
}
How to Use It
- Create the Asset: Right-click in your Project window, select
Events > Void Event Channel, and name it something likePlayerDeathEvent. - The Broadcaster: Your
PlayerHealthscript simply holds a reference to this asset. When health hits zero, it callsPlayerDeathEvent.RaiseEvent(). - The Listener: Your
UIManagerorSoundManagerscript holds the same reference. InOnEnable, it subscribes toPlayerDeathEvent.OnEventRaised += HandleDeath;.
Because this is a ScriptableObject, you don't have to worry about finding objects in the scene or complex GetComponent calls. You simply drag the PlayerDeathEvent asset into the slots of any script that needs to listen or broadcast. This keeps your architecture clean, modular, and easy to debug.
Key Takeaway: ScriptableObject Event Channels allow you to decouple game systems by acting as a shared communication bridge that objects reference as an asset rather than a direct scene dependency.