Events in Unity

Natalia DaLomba
Women in Technology
3 min readAug 15, 2023

Unity events work in a similar way to delegates except that while you’d normally set up delegates in a script, Unity events allow you to create modular functionality in the inspector instead. Chances are, you’ve used Unity events before as the drag and drop event system that UI buttons and sliders use — that is based on Unity events.

However, while you may be used to seeing them on UI components, they can also be used to create modular connections between scripts. This can be extremely useful for setting up different objects in different ways using the same scripts. For example, imagine that you have a box and an enemy. You could use the same script to allow both objects to take damage and when they run out of health and then each are destroyed.

Although, what happens next might be different for each object. For instance, destroying the box might drop loot while killing the enemy might give you experience. The trigger is the same but the results are different.

So how does it work?

To use a Unity event, you’ll need to add the UnityEngine.Events namespace then declare a public Unity event for the event you want to create and trigger it using the invoke method.

using UnityEngine.Events;

public class CollisionDamage : MonoBehaviour {
//code

public UnityEvent onDamageTaken;

private void OnCollisionEnter2D(Collision2D other) {
if (velocity > minVelocity)
//don't need to check to see if unity event is null
onDamageTaken.Invoke();
}
}

Unlike delegates, you won’t need to check to see if the Unity event is null before calling it. Then in the inspector, you can connect the Unity event to other scripts and objects — either to call a method, turn something on or off, or do nothing. This is because the advantage of using a Unity event like this is that different objects can do different things with the same trigger.

When connecting a Unity event to a method, by default Unity events pass static parameters. This means that the value or reference that is given to the method if it takes one is whatever you set in the inspector. But, it’s also possible to create Unity events that pass dynamic data.

For instance, a Unity event is called where an object is damaged could also be set up to pass the amount of damage taken as a float. This works by creating your own serializable class that inherits from Unity events. This allows you to create a type of Unity event that accepts up to four parameters by specifying what values the event will take in angled brackets. Then use your custom Unity event in place of a regular one and select the dynamic value option when connecting it to a script.

[System.Serializable]
public class UnityFloatEvent : UnityEvent<float> { }

public class CollisionDamage : MonoBehaviour {
//custom Unity event
public UnityFloatEvent onDamageTaken;

private void OnCollisionEnter2D(Collision2D collision) {
//code

if (velocity > minVelocity)
//dynamic value option
onDamageTaken.Invoke(velocity * damageMultiplier);
}
}

Unity events are great for creating modular points of contact that can be
used to connect objects together in different ways.

However, because they involve connecting scripts manually in the inspector, they usually work best for interactions between local objects such as scripts on the same object or group of objects within a prefab. Then on the other hand, static event delegates can be used in code to manage game-wide global events that affect multiple other objects. But what if you wanted to combine the benefits of working with Unity events in the inspector with global game-wide events?

This is where you would use Scriptable Objects.

Check out my Using Scriptable Objects for Events article.

Much credit goes to John at Game Dev Beginner. His full article is here. Also, take a look at his blog to learn more about using C# in Unity.

--

--

Natalia DaLomba
Women in Technology

A Unity C# developer inspired by game design logic used to create digital adventures. https://www.starforce.games/devlog/