The Basics Of Delegates In Unity

Delegates are generally considered an advanced topic in C#, but in actuality, they are really very simple. All a delegate does is allow a method to be used as a variable.

This will run Method1, then Method2, by calling the method SomeDelegate

Already you can probably think of some interesting uses for delegates, but the reason they are an advanced topic is that they unlock a lot more than they appear to at first. As far as I have seen, the main usage for delegates seems to be events.

While this object is enabled, we listen for someEvent, and doSomething when it happens

This is a powerful use case for delegates, allowing you to subscribe and unsubscribe from events. If you made this event public, you would be able to listen for it from anywhere in your scene without any explicit references! You could do this without the event keyword as well, but it’s best practice not to because an event can’t be instantiated directly or overridden. There is a whole lot more you can do with delegates beyond just this, but if you’re still new to C# getting a handle on the basics of delegates and events is a great start, and should make building your project a lot easier.

Game developer with over 12 years of experience, mainly focused on MMOs.