The Power of Events and Delegates

4 min readApr 12, 2025

Event delegates allow you to create fully self-contained and independent pieces of code, leading to highly efficient and modular scripts. Let’s take a look at how they can work to your advantage.

Objective: Understand how to create event delegates and explore their practical benefits.

Table of Contents

· What is an Event?
· Creating an Event — Broadcaster
· Objects that Communicate with the Event — Listeners
Setting up the Scene Elements
The Cube Script
The Result
How is this useful? Practical Examples

What is an Event?

An event allows one part of your code to signal that something has occurred, enabling other parts of your program to respond — without needing a direct connection.

Key Points

You can imagine it like a signal being broadcast:

A script triggers the event when a specific action happens (like a player collecting an item).

Any scripts that are listening for that event can then run their own methods in response (such as updating the UI or playing a sound effect).

Under the hood, events rely on delegates, which define the required method signature for any functions that want to respond.

Creating an Event — Broadcaster

As previously stated, events rely on delegates so let’s start by creating a script called Main and adding a delegate, and then an event.

Note: You can learn about delegates here.

We use a static event because can be accessed without creating an instance of the class. This is useful when you want a global notification system, where any part of your code can raise or subscribe to the event without worrying about object references.

Then we create a public void method that will hold our executable code. For my code, I am using a button.

Key Points:

→ It’s crucial to check whether the event is not null — that is, to ensure there are listeners subscribed to it. If you invoke an event with no subscribers, it can result in a runtime error.

→ We can call our onClick event once a button has been pressed.

Now we need some listeners to subscribe to our broadcast.

Objects that Communicate with the Event — Listeners

Setting up the Scene Elements

Our listeners will be three cubes, each with a new script attached called “Cube”. This script is independent to our Main script, which is where the delegate and event have been coded. I’ve also added a UI Button.

I have attached the Main script to the Main Camera, then added the Main Camer to the On Click slot in my Button. This will allow me to add my ButtonClick() method as an On Click response.

The Cube Script

In the cube script, we’re going to create a method to turn the cubes red.

Since we created a public static event, we can access it directly in this script.

How it works:

  • Any object that accesses the event via script becomes a listener that is now subscribed to the event broadcast.

What that means:

  • Any object with the onClick event will “listen” for any button being clicked on your scene. So, whenever a button is clicked, the objects will respond by turning red.

The Result

How is this useful? Practical Examples

Here are some practical examples to visualize how useful this can be:

  • Imagine you have hundreds of objects that need to turn red or change in some way. Instead of modifying each one individually or relying on a performance-heavy for-loop, you can have them all subscribe to a single event and respond simultaneously when that event is triggered.
  • As mentioned earlier, these objects remain self-contained and independent. If you want each one to react differently to the same trigger, that’s totally possible — because the event doesn’t dictate how they respond, just that they respond. Each object can define its own unique reaction within its own script.
  • For example, in a game with multiple enemy types, you might want all enemies to react when their health drops below 50%. By broadcasting a single event when this condition is met, all enemies can listen for it and respond with their own custom behavior — like changing tactics, retreating, or becoming more aggressive — without needing individual triggers for each type.

I hope this article was helpful! Check out my portfolio! Looking to get hired in the video game industry! :)

--

--

Dennisse Pagán Dávila
Dennisse Pagán Dávila

Written by Dennisse Pagán Dávila

Software Engineer that specialize in Game Development. Currently looking for new opportunities. Portfolio: dennissepagan.com

No responses yet