Unity Learn home
View Tutorial Content
Steps

Delegates

Tutorial
Intermediate
+10 XP
5 Mins
(1917)
Overview
Skills
Summary
How to create and use delegates to provide complex and dynamic functionality in your scripts.
Select your Unity version
Last updated: January 12, 2022
2019.3
2019.2
2019.1
2018.4
2018.3
2018.2
2018.1
2017.4
2017.3
2017.2
2017.1
5.x
4.x
Language
English
Also included in

1.Delegates

Video Player is loading.
Current Time 0:00
Duration 4:47
Loaded: 0%
Stream Type LIVE
Remaining Time 4:47
 
1x
English
00:00:01
- [Instructor] Delegates allow you to create
00:00:02
robust and complex behaviors in your scripts.
00:00:05
A delegate can be thought of simply as
00:00:07
as a container for a function
00:00:09
that can be passed around or used like a variable.
00:00:12
Just like variables,
00:00:13
delegates can have values assigned to them
00:00:15
and these values can be changed at run time.
00:00:18
The difference is that while variables contain data,
00:00:21
delegates contain functions.
00:00:24
Here we have a scene with a game object in it.
00:00:26
The game object has a delegate script attached to it.
00:00:29
We will be monitoring the output of the scene
00:00:32
in the console view.
00:00:34
In the delegate script,
00:00:36
the first thing we do is to clear our delegate template.
00:00:39
This template will dictate exactly what types of
00:00:40
methods we can assign to our delegate.
00:00:44
We create a delegate with the delegate keyword.
00:00:47
What follows the delegate keyword
00:00:49
is the delegate's signature.
00:00:51
Just like a function,
00:00:52
our delegate has a return type, a name,
00:00:55
and a parameter list.
00:00:57
In this example, we can see that in order
00:00:59
for a method to be assigned to this delegate,
00:01:01
it must have a return type of void
00:01:03
and take a single integer parameter.
00:01:07
After we create our delegate type,
00:01:09
we then declare a member variable.
00:01:11
This member variable has the type
00:01:13
of the delegate we just created.
00:01:16
At the bottom of our script, we have two methods.
00:01:19
Print Num and Double Num.
00:01:22
We can see that each of these methods
00:01:23
has a return type of void,
00:01:25
and takes a single integer parameter,
00:01:26
just like our delegate.
00:01:28
Additionally, each of these methods
00:01:30
does something slightly different
00:01:32
with the integer data passed in.
00:01:35
Now it's time for us to see exactly what delegates do.
00:01:39
In the start method,
00:01:40
you can see that we assign the name of
00:01:42
our PrintNum method,
00:01:43
to the myDelegate variable.
00:01:46
We then use the myDelegate variable
00:01:48
as if it was a function.
00:01:49
And we pass in the value of 50.
00:01:53
We then assign the name of the DoubleNum method
00:01:55
to the myDelegate variable.
00:01:58
Again we call it just like a function.
00:02:01
Let's take a look at this code in action.
00:02:04
Back in unity,
00:02:06
we can run our scene and look at the console view.
00:02:09
We can see that we are able
00:02:10
to call two different methods
00:02:12
using the same delegate variable.
00:02:15
This gives us a lot of power
00:02:17
to dynamically control which functions
00:02:19
get called in our games.
00:02:22
Delegates also have the ability to be multicast.
00:02:26
Multicasting allows a single delegate variable
00:02:28
to represent multiple methods at the same time.
00:02:31
Here we have a scene with an orb in it.
00:02:34
The orb has a script called multicast script attached to it.
00:02:39
In our multicast script,
00:02:41
we can see that we have created a delegate template.
00:02:44
This template finds a delegate named MultiDelegate
00:02:47
which takes no parameters and has a return type of void.
00:02:52
Next we create a member variable named myMultiDelegate,
00:02:56
that is of the type
00:02:57
of the delegate template we just created.
00:03:01
At the bottom of our script,
00:03:02
we have two methods named PowerUp and TurnRed.
00:03:06
Both of these methods take no parameters,
00:03:08
and have a return type of void,
00:03:10
just like our delegate type.
00:03:13
The PowerUp method prints
00:03:14
"Orb is powering up" to the screen.
00:03:17
The TurnRed method changes the color of the object to red.
00:03:23
In the start method,
00:03:24
we will multicast our delegate variable.
00:03:27
We do this by assigning both the power up method
00:03:30
and the turn red method to the same delegate variable,
00:03:33
using the += operator.
00:03:38
In this way, the variable, myMultiDelegate, contains
00:03:42
both the PowerUp and TurnRed method.
00:03:46
We then call the variable myMultiDelegate,
00:03:48
as if it were a function.
00:03:51
If we go back into Unity and run our scene,
00:03:54
we can see that multicasting our delegate variable
00:03:56
allowed it to call both the PowerUp and the TurnRed methods
00:04:00
with a single call.
00:04:02
In this manner, we were able to stack functionality.
00:04:07
If we want to remove a method from a delegate variable,
00:04:10
we can do so using the -= operator
00:04:12
in conjunction with the method's name.
00:04:16
One thing we must be careful of
00:04:18
is attempting to call a delegate variable
00:04:19
like a function before we have assigned anything to it.
00:04:23
Doing so will cause an error, and we like to avoid those.
00:04:27
Any delegate variable that doesn't currently
00:04:29
have a method assigned to it,
00:04:31
will have a value of null.
00:04:33
Therefore, it is a good idea to always check to make sure
00:04:37
delegate does not equal null before using it.
Delegates
4 Mins 48 Secs

DelegateScript
using UnityEngine; using System.Collections; public class DelegateScript : MonoBehaviour { delegate void MyDelegate(int num); MyDelegate myDelegate; 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); } }

MulticastScript
using UnityEngine; using System.Collections; public class MulticastScript : MonoBehaviour { delegate void MultiDelegate(); MultiDelegate myMultiDelegate; void Start () { myMultiDelegate += PowerUp; myMultiDelegate += TurnRed; if(myMultiDelegate != null) { myMultiDelegate(); } } void PowerUp() { print ("Orb is powering up!"); } void TurnRed() { renderer.material.color = Color.red; } }

Delegates
Delegates
General Tutorial Discussion
0
0
1. Delegates
52
10