In C#, delegates are the containers for functions that behave like reference pointers to a method that can be used as a variable in your program. You can use C# delegates to make your code efficient and modular specially when you have a rapidly growing code base.

You can assign value to delegates and can change them at runtime. Even if you're a beginner programmer, I'm pretty sure that you know about the subscription-based services where you can subscribe to a channel, or an author. When you subscribe,  you will receive the automatically receive updates from them. Delegate works exactly in that way, where the passed method gets called when subscribed and vice versa.

Practical Use case of C# Delegates

As we know objects can easily be sent as parameters in the method. But sometimes we might feel the need to be sent a method as a parameter of another method that's when we will need the delegates.

Type of Delegates

There are two types of delegates.

  1. Single Delegate – A single delegate can reference only one method at a time.
  2. Multicast Delegates – A multicast delegate can store references of multiple methods at a time.

Examples of using C# Delegates in Unity

Here are some of the examples that will help you understand the concept better.

Declare a Delegate

public delegate void ChangeProperty();
public static ChangeProperty ONChangeProperty;
Defining Delegates

Subscribe to Delegates

// single cast delegates
DeligateTester.ONChangeProperty = UpdateColor;

// multicast delegates
DeligateTester.ONChangeProperty += UpdateColor;
DeligateTester.ONChangeProperty += UpdatePosition;
Subscribing to Delegate

Calling Delegates

DeligateTester.ONChangeProperty();
Delegate Call

Unsubscribing from a Delegate

private void OnDisable()
{
	DeligateTester.ONChangeProperty -= UpdateColor;
	DeligateTester.ONChangeProperty-= UpdatePosition;
}
Unsubscribe Delegate

More Examples

Now, let's put everything together and see in action. Let's create the new class named DelegateTester and attach the script to an empty game object.

using UnityEngine;

public class DeligateTester : MonoBehaviour
{

    // defining delegates
    public delegate void ChangeProperty();
    public static ChangeProperty ONChangeProperty;
    
}
Delegate Tester

Now create a new cube and attach the new script named ObjectController. And add the below code to that script.

using UnityEngine;

public class ObjectController : MonoBehaviour
{
    public Material renderermt;
    private void Start()
    {
        renderermt = GetComponent<Renderer>().material;
           // subscribing to delegates
           // single cast delegates
           DeligateTester.ONChangeProperty = UpdateColor;
           
           // multicast delegates
           DeligateTester.ONChangeProperty += UpdateColor;
           DeligateTester.ONChangeProperty += UpdatePosition;
   
           // calling delegates
           DeligateTester.ONChangeProperty();
           
         
       }
   
       public void UpdateColor()
       {
           renderermt.color = Color.blue;
       }
       public void UpdatePosition()
       {
           transform.position = new Vector3(transform.position.x + 5f, transform.position.y + 5, transform.position.z);
       }

       private void OnDisable()
       {
           // unsubscribing delegates
           DeligateTester.ONChangeProperty -= UpdateColor;
           DeligateTester.ONChangeProperty-= UpdatePosition;
       }
}
Object Controller

When you run the game you will see it will call the assigned method. It concludes that any class can subscribe ONChangeProperty delegates and get a callback when this gets called.

Thank you for reading till the end. You can leave comments if you have any queries or feedback.