Delegate, Event, and Action

Before continuing to talk about Unity’s new Input System, it seems appropriate to talk about Event, Delegate, and Action.

In Unity many of the things we already use in an event system, in general, all the experiences that exploit the UI are driven by events that wait for the occurrence of a certain behavior, arrival of input, for example, pressing a virtual button to trigger an effect. Basically, they are listening, waiting to be called. The program does not end when the instructions are finished, but when the event that will make it end is called.

There are several ways to implement it, we can use a delegate, event, and Action. We will see that in addition to the keywords some behaviors are different. In my example, I will always have a script related to the main camera and a script on the cube but it could be any game object in the scene.


when we use a delegate we must first declare it with the keyword “delegate” and assign to it the type we want to return and the name followed by brackets. Then we have to create an element of the type of delegate we have created and give it a name. We use the keyword “static” to make it always accessible without having to go to recall the component.
At the start, we are going to check if there are elements in our delegate and if we find them we are going to execute them.

in the script of the cube, we define the method that will be called and we assign it to the delegate at the start, which will then be able to execute it.
Note: the delegate and the method must have the same signature, there must be the same number and type of parameters to make it work.


we have to declare a delegate as we did before, but in the creation of the element, in addition to the keyword static, we also add the keyword event.

also here the rules are the same we have seen before, but to assign the method written in the cube class to the event we have to use the operators “+=” this allows us to add an element to the list of methods that must be called when the event occurs. Unlike delegates this type of assignment allows us to avoid that things are overwritten but can only be added or removed.


With the actions, we can remove a step by defining at once delegate and delegate element by defining the type of parameter we want to pass in the angle brackets.

the assignment remains as in the case of the event and the rules are always the same, that is the signature of the method must correspond to the parameters of the Action otherwise our editor will report an error.

Best Practise

as you can see from the images my delegate, event and Action are always preceded by an if statement that verifies that it is not null, this allows us to avoid compiler errors and to launch our event only when someone is registered.

OnEnable and OnDisable
We are building a system of methods that are called automatically when certain conditions occur, we must be sure that this happens only when it is really necessary. A good way to do this is to subscribe to the events when the game object is actively inserted in the scene and unregister it when it is disabled or removed using the methods OnEnable and OnDisable.

I hope this article has been helpful. See you next time



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Andrea Alicino

Andrea Alicino

Unity developer and C# programmer. Currently looking for games industry employment.