I have started learning Unity in 2019, and Events were the part that felt me overwhelmed — which slowed my pace of learning for some time. In this article, I will be focusing on what are delegates, events and actions and how to make use of them.
Delegates are method type variables. Meaning, it is a pointer of a method. Here’s a pretty simple example in order to gather a brief understanding of the delegates:
On the example above, I have defined two delegates; one with no parameters and one with a color parameter. As stated, delegates are just variables that requires methods as a value. In the Start method, we simply set the delegates to methods with appropriate parameters. Meaning, we cannot set the delegate value to a method that does not match with its parameter requirement — as expectedly.
Delegates can point to one or multiple methods. The delegates that include multiple methods is called multicast delegates.
So why do we need delegates? Well, delegates act as a broadcast system that allows other game objects to subscribe to them. You can think this as a simple subscription system. Once the delegate is called, the game objects that are subscribed to the delegate are being triggerred.
Simply put, events are type of delegates. By using an event, we add a protection layer to the delegate. Events only allow the classes to subscribe or unsubscribe to the delegate. Events cannot be assigned to any method. The methods can only be added or substracted from an event.
Delegates and events acts as a basis for the Observer Design Pattern. Using delegates and events, we can call any object within the scene, without requiring any reference to any game object.
In the example below, the color of the cubes will be changed by click of a button, without referencing. First, we define a delegate and then an event from the delegate. Before calling the event, we must make sure that there are listeners by null checking (if there are no listeners, we will get a null reference error).
Now, we can attach a script to the cubes and register method(s) to the event. Certainly, method signatures must be matching with the event signature:
As can be seen above, we have used “+=” to add the method to the event. Since this script is attached to the all of the cubes within the scene, the only thing left is to add the Main.ButtonClick method as the onClick event to the button.
The process of assigning a game object behaviour to trigger of a particular event is called subscribing to an event. In order to call a method within a game object according to an occurrence of an event, it has to subscribe to the event.
The thing here is that any of the game objects did not have to know each other at all. In a scenario with tens of game objects, it would be chaotic. Rather, it listens to the event that is being invoked with a button click.
Likewise, it is crucial to unsubscribe the game object which is not active — as it will cause an error by trying to provide a result with the event trigger. Usually, we unsubscribe from the event once the game object is disabled.
Another example is provided below, which updates the death count once the player dies:
As you can observe, neither of the GameManager and UIManager has a reference to the Player. Rather, they are being triggerred once we hit the space key.