i was reading about delegates, and think i get it why they are for, just want to know how you use it in your project. where do you instantiate delegate? can somebody give a simpliest example of the use in unity? thank you
Delegates are part of the C# language specification. They are basically callbacks.
You can read Microsoft reference, or look at this tutorial.
Define a signature for your callbacks:
delegate void MyDelegate(int myInt);
Create a container:
public MyDelegate myDelegate;
Create a method matching the delegate signature:
void MyMethod(int myInt)
{
Debug.Log(myInt);
}
Add this method to the delegate collection:
myDelegate += MyMethod;
To call all callbacks that have been registered, just use it like a C#-method:
if (myDelegate != null)
{
myDelegate(42);
}
This will fire MyMethod(42) and print the log. Now the magic comes with the fact that myDelegate is public. Therefore, you can add (or remove) callbacks from any other script.