Delegates and Events (Part 1/4): What is a Delegate?

Simon Pham
3 min readMar 27, 2024

--

What is a delegate?

A delegate can be thought of simply as a container for a function that can be passed around or used like a variable. Just like variables, delegates can have values assigned to them, and these values can be changed at run-time. The difference is that, while variables contain data, delegates contain functions.

How do I declare and use a delegate?

First, we need to declare our delegate template which will dictate exactly what types of methods we can assign to our delegate. We start with the “delegate” keyword, followed by the delegate’s signature which includes a return type, a name, and a parameter list.

delegate void MyDelegate(int num);

As you can see, in order for a method to be assigned to this delegate, it must have a return type of void and take a single integer parameter. After we create our delegate type, we then declare a member variable which has the type of the delegate we just created.

MyDelegate myDelegate;

Next, let’s create two simple methods that can print out numbers.

void PrintNum(int num)
{
print("Print Num: " + num);
}

void DoubleNum(int num)
{
print("Double Num: " + num * 2);
}

As you can see, both of our methods have a return type of void and accept a single integer parameter which matches with our delegate. Now we can assign the name of the methods to the myDelegate variable and use it like a function.

private void Start()
{
myDelegate = PrintNum;
myDelegate(50);

myDelegate = DoubleNum;
myDelegate(50);
}

Here’s the result:

And here’s the full code:

using UnityEngine;

public class Main : MonoBehaviour
{
delegate void MyDelegate(int num);

MyDelegate myDelegate;

private void Start()
{
myDelegate = PrintNum;
myDelegate(50);

myDelegate = DoubleNum;
myDelegate(50);
}

void PrintNum(int num)
{
print("Print Num: " + num);
}

void DoubleNum(int num)
{
print("Double Num: " + num * 2);
}
}

Multicast

Multicasting allows a single delegate variable to represent multiple methods at the same time. First, I’m gonna declare a delegate template and a member variable.

delegate void MultiDelegate();
MultiDelegate myMultiDelegate;

Next, I’m gonna create two methods.

void PowerUp()
{
Debug.Log("Player is powering up!");
}

void TurnRed()
{
GetComponent<MeshRenderer>().material.color = Color.red;
}

The PowerUp method prints out a message while the TurnRed method turns the player red. To multicast our delegate variable, we assign both methods to it using the += operator.

private void Start()
{
myMultiDelegate += PowerUp;
myMultiDelegate += TurnRed;

myMultiDelegate();
}

To remove a method from a delegate variable, we can use the -= operator.

myMultiDelegate -= PowerUp;
myMultiDelegate -= TurnRed;

Calling a delegate variable before assigning anything to it will result in an error. To avoid this, it’s good practice to null-check before calling a delegate variable.

Here’s the full code:

using UnityEngine;

public class Multicast : MonoBehaviour
{
delegate void MultiDelegate();
MultiDelegate myMultiDelegate;

private void Start()
{
myMultiDelegate += PowerUp;
myMultiDelegate += TurnRed;

if(myMultiDelegate != null)
{
myMultiDelegate();
}
}

void PowerUp()
{
Debug.Log("Player is powering up!");
}

void TurnRed()
{
GetComponent<MeshRenderer>().material.color = Color.red;
}
}

In the next part of this article, we’ll be looking at events in Unity.

--

--