• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by sdgd · Dec 20, 2013 at 12:26 PM · c#eventsdelegatesoafa

How do Delegates and Events work?

I've looked around and saw few examples and tutorials, each one was explaining differently and I couldn't understand how exactly do they work.

so:

  • What is Delegate

  • What is Event

  • How to use Delegate

  • How to use Event

I'm posting t$$anonymous$$s Q&A as I saw non other to fit logically how they work at least in my mind.

if you are wandering why OAFAT title? It might be first but I hope it won't be last.

Comment
hdeekshith

People who like this

1 Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jamora · Dec 20, 2013 at 07:43 PM 1
Share

It's not the first one. I've been on the lookout for good Questions and Answers for the oafa -tag and this certainly is worthy to be the 4th. Instead of the title, I use the tag system; you can easily find all questions with the same tag by clicking the tag.

avatar image sdgd · Dec 20, 2013 at 11:48 PM 0
Share

thanks didn't know about that system, ...

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer Wiki

Answer by sdgd · Dec 20, 2013 at 12:27 PM

  • Delegate is a container of events (we could say array)

  • Events are container of pointers to functions (we could say array)

I'll try to write as many different names as possible so you can see how it works.

 using UnityEngine;
 using System.Collections;
 
 public class EventManager : MonoBehaviour {
     public delegate void DelegateContainer ();
     public static event DelegateContainer EventContainerOne; // t$$anonymous$$s is when DelegateContainer adds event container to $$anonymous$$m self
     public static event DelegateContainer EventContainerTwo;

     public bool EditorTriggerOne = false;
     public bool EditorTriggerTwo = false;
     public bool EditorTriggerReset = false;

     void Update(){
         if (EditorTriggerOne){
             TriggerOne();
             EditorTriggerOne = false;
         }
         if (EditorTriggerTwo){
             TriggerTwo();
             EditorTriggerTwo = false;
         }
         if (EditorTriggerReset){
             Reset();
             EditorTriggerReset = false;
         }
     }
     
     // NormalFunctions
     public void TriggerOne(){
         // if there's no pointers to functions in EventContainerOne it'll give us NRE error
         if (EventContainerOne != null){
             // we fire all functions inside t$$anonymous$$s EventContainer
             EventContainerOne();
         }
     }
     public void TriggerTwo(){
         if (EventContainerTwo != null){
             EventContainerTwo();
         }
     }
     // here we remove all functions from events
     public void Reset(){
         EventContainerOne = null;
         EventContainerTwo = null;
     }
 }


Random Scripts that Want the behaviour:

 using UnityEngine;
 using System.Collections;
 
 public class RandomScript : MonoBehaviour {
     void Start(){
         EventManager.EventContainerOne += NormalFunctionOne;
         EventManager.EventContainerTwo += NormalFunctionTwo;
         EventManager.EventContainerTwo += NormalFunctionThree;
     }
     void NormalFunctionOne(){
         Debug.Log("Func 1");
     }
     void NormalFunctionTwo(){
         Debug.Log("Func 2");
     }
     void NormalFunctionThree(){
         Debug.Log("Func 3");
     }
 }

Now each time you trigger one func 1 will appear.

Each time you trigger two func 2&&3 will appear.

T$$anonymous$$s is because there are 2 pointers inside EventContainerTwo.

Another thread regarding events&delegates and their uses can be found here

Sources:

  • YoutubeVideo

  • Official Unity Tutorial

  • UnityForum

  • CommonQuestion

Comment
robertbu
stevethorne
forsamori
Eleom
hdeekshith
diegzumillo
MaydanV9

People who like this

5 Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image vexe · Dec 20, 2013 at 08:24 PM 0
Share

I think you got the terms a 'little bit' mixed up :) - It's actually the delegate that's a pointer to a function. An event is a delegate itself. More specifically, it's a multicast delegate. Meaning you can't assign an event: onPlayerDied = OnPlayerDiedHandler; instead you have to: onPlayerDied += OnPlayerDiedHandler; - in delegates, you don't have that constraint.

avatar image vexe · Dec 20, 2013 at 08:27 PM 0
Share

I highly recommend going over this series from the beast Jamie king about delegates, events, lambda expressions, anonymous types, etc. http://www.youtube.com/playlist?list=PLAE7FECFFFCBE1A54

avatar image sdgd · Dec 20, 2013 at 11:47 PM 0
Share

and what is

 onPlayerDied

 OnPlayerDiedHandler

can't you be more specific to this answer than to links I've given?

and can you write in code please and not in salami it's really hard to read.

and above all BTW it's marked for unity wiki and you can change it.

and the code above works exactly as I said, ... I verified it again.

if you trigger 1 you get debug 1 and if you trigger 2 you get 2&&3

and I will try doing it without delegate, ... as I said just started learning this, ...

but you can as always change my answer, ...

avatar image vexe · Dec 21, 2013 at 12:00 AM 0
Share

maybe after I wake up from my coma. But the youtube list ^ covers it all.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Subscribers of Delegates? 2 Answers

Question on Events and Delegates 1 Answer

Stack Overflow error on delegate call 2 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges