Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
1
Question by toborific · Oct 20, 2013 at 01:45 AM · eventsevent-handling

Event Manager and Event Listener

I've read through documentation on events and watched a couple tutorials, but there is something I'm still not grasping. In the examples I've seen of Event Managers, the method that fires the event is in the same class as the Event Manager. Like this:

 using UnityEngine;
 using System.Collections;
 
 public class EventManager : MonoBehaviour {
     
     public delegate void CheckpointHandler(int id);
     public static event CheckpointHandler checkpointReached;
 
     void OnGUI () {
     
         if( GUI.Button(new Rect(5,5,150,40),"Checkpoint")){
             checkpointReached(555);
         }
         
     }
 
 }

So here, the EventManager not only defines the delegate and event, but also creates a button which fires the event.

This is what confuses me. Somehow I thought the EventManager would do nothing but define the events. Then you would fire those events from other scripts. Is there a way to do that?

I don't like the way this example is set up because it seems that I would have to attach an "EventManager" script to every object that could possibly fire an event. What if there are multiple objects that can fire the same event? Do I need to attach the same "EventManager" to every one?

Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by daklab · Nov 24, 2013 at 06:51 PM

You can achieve this without dragging and dropping the EventManager instance into each script that needs it. You can check this post: (what you're looking for would be at the bottom): Quick Tip: An Efficient Event Delegation Approach in Unity

Comment
Add comment · Show 2 · 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 asperatology · Jun 08, 2015 at 05:56 PM 0
Share

Linked site is giving me 404.

avatar image daklab asperatology · Apr 22, 2016 at 02:34 PM 0
Share

Thank for the heads up, I updated the URL.

avatar image
2

Answer by AshwaniKumar · Jan 24, 2015 at 03:38 AM

I have a blog post on this which describes how to create a simple event system in Unity3d. You can check it here.

Comment
Add comment · 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
0

Answer by toborific · Oct 20, 2013 at 02:48 AM

I think I may have answered my own question, but maybe someone can tell me if this will perform well or not.

I set up the event triggers in EvenManager as public methods, and then I used the drag-drop method to place the EventManager in other classes, making its methods accessible to anything that wants to fire an event. So my setup looks like this (all of these are attached to separate game objects):

 using UnityEngine;
 using System.Collections;
 
 public class EventManager : MonoBehaviour {
 //Here I define the delegates, events and the triggers that can be called to fire the event.
     
     public delegate void CheckpointHandler(int id);
     public static event CheckpointHandler checkpointReached;
 
     
     public void hitCheckpoint (int id){
         if (checkpointReached != null)
             checkpointReached(id);
     }
     
 
 }


 using UnityEngine;
 using System.Collections;
 
 public class EventTrigger : MonoBehaviour {
 /******
 In this class I can set up a method that will access the EventManager 
 (which has been dropped onto the em variable) 
 and fire the hitCheckpoint method, 
 which in turn sends the checkpointReached event.
 ******/
     
     
     public EventManager em;
 
     void OnGUI () {
     
         if( GUI.Button(new Rect(5,5,150,40),"Checkpoint    ")){
             em.hitCheckpoint(555);
         }
         
     }
 }


 using UnityEngine;
 using System.Collections;
 
 public class EventListener : MonoBehaviour {
     //This script listens for the event and prints a message to the log.
     
     void OnEnable () {
         EventManager.checkpointReached += HandleEventManagercheckpointReached;
     }
     
     void OnDisable(){
         EventManager.checkpointReached -= HandleEventManagercheckpointReached;
     }
 
     void HandleEventManagercheckpointReached (int id)
     {
         Debug.Log ("Checkpoint reahed. ID is: " + id);
     }
     
 
 }
Comment
Add comment · 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

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.

Follow this Question

Answers Answers and Comments

17 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

Related Questions

Problem with adding listeners on spawm 0 Answers

Can other scripts use event triggers from event manager? 0 Answers

Why does my code hang when calling a script in a multi-nested event function 0 Answers

Problems between GameEventManager and LoadScene 2 Answers

Where can I find complete list of events? 1 Answer


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