Events

Events are ScriptableObjects which contain one event, either with one or no argument. You can think of them as channels that any object, from any scene, can tap into.

Additionally, they present a button in their Inspector that allows to fire the event on demand, for testing purposes.

Sample usage 💡

In an action game, a player character Prefab could broadcast its death on a "OnPlayerDied" SO Event. Any object in the game that needs to react to it, all the way up to a game manager, can reference into that SO to react.


Using Events

Creating a new Event

To create a new Event, simply position your mouse in the Project View and right click, then Create > ScriptableObject Tools > Events, and then choose the type you want to create.

Referencing an Event in scripts

Like usual, expose a public or serialised property of the right type:

public VoidEvent playerDiedEvent;
// Or also
[SerializeField] private VoidEvent _playerDiedEvent;

Listening to an event

The event contained in the ScriptableObject is not public, so to hook an object to an event, you can use the syntax as if the SO was the event itself.

playerDiedEvent += RespawnCharacter;
// and later...
playerDiedEvent -= RespawnCharacter;

Firing an event

To fire on a SO Event, use the Invoke() method, passing the correct arguments:

playerDiedEvent.Invoke();
playerHealthChanged.Invoke(-3);

Extending Events

Extending the Event system with a new class is simple, but its difficulty varies whether you want to broadcast just a different type, or potentially multiple arguments.

New event with single argument

To create a new event that carries one argument, all you need to do is to create a new C-sharp file subclass EventOneArg<T> like this (we'll use Collider as an example):

[CreateAssetMenu]
public class ColliderEvent : EventOneArg<Collider> { }

For this case, nothing else is needed, and the event SO will even get the Invoke event button automatically.

New event with argument of an "unsupported" type

If you inherit from EventOneArg<T> and use type that our custom Inspector doesn't immediately support, like for instance a Struct.

This is because since you subclassed EventOneArg<T> you also get the customised generic Inspector. But with the unsupported type, the Inspector breaks.

This doesn't mean that you can't use events with that type, but you will have to put together a custom Inspector - or just use the default one. For instance, say we defined a struct:

public struct MyStruct
{
    public int firstProp;
    public int secondProp;
}

And then created an event like this:

[CreateAssetMenu]
public class StructEvent : EventOneArg<MyStruct> { }

To ensure that this class stop using the customised Inspector, you can simply write this:

[CustomEditor(typeof(StructEvent))]
public class StructEventEditor : Editor { }

Just remember to put this code in an Editor folder, since it's using Editor classes that will fail in case you create a build.

New event with two or more arguments

It's also possible to add an event with two or more arguments, but this guide doesn't cover it. You can use EventOneArg<T> and one of its inheritors as a guide.

Last updated