• 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 KristofferH · Apr 02, 2020 at 12:31 PM · eventsdelegates

Multiple kinds of "triggers" raising the same event?

With delegates I can have multiple listeners listening to the same "trigger" (I'm not necessarily talking about Unity Colliders set to isTrigger, even though that is one type of trigger in t$$anonymous$$s case), and when the event is raised they all "hear" it and react to it accordingly. But can I have it the other way around? Multiple "triggers" raising the same event but only one listener reacting to it?

So in my case I have a Timeline PlayableDirector that I want to be able to trigger to play in multiple ways. So I want an listener that just listens for that event, and does not care where it came from; it could be a keyboard key being press, or you click on somet$$anonymous$$ng in the scene, or you click on an UI element, or you enter a collider in the scene.

I can ofcourse use public properties that just references the PlayableDirector but then I have to make sure to set those references. I wan't the decoupling feature that delegates/events offer. So I want to be able to just add a trigger in the scene that when triggered raises an event, and if no one is listening to that event then not$$anonymous$$ng happens, but perhaps later on the listener will we spawned into the scene and now if you activate the trigger again somet$$anonymous$$ng will be listening to that event and then act accordingly.

Like your TV; you can use the remote to turn it on, or you could press a button on the TV to turn it on, or you counld use your smartphone app to turn it on. The TV does not care where the signal to turn it on came from, it just execute the command - no questions asked :)

Comment

People who like this

0 Show 0
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

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by FlaSh-G · Apr 03, 2020 at 01:15 AM

The idea be$$anonymous$$nd events (in general) is that senders and responders don't know the other side. They might share semantics so both sides know what the parameter means if you have one, but the senders don't know w$$anonymous$$ch (or even how many) responders there are, and the responders usually don't know who sent the event.

Having mutliple senders is easy - anyone can trigger an event. Picking a subset of the responder group however means implementing some sort of logic somewhere that determines who gets to respond. You can do t$$anonymous$$s by writing a bit of code in your class that has the event.

For example:

 // Add to and remove from t$$anonymous$$s list with public methods
 private static readonly List<Action> responses = new List<Action>();
 
 public static TriggerSingleRandomResponse()
 {
   responses[Random.Range(0, responses.Count)]();
 }
Comment

People who like this

0 Show 0 · 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.

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

127 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 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 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 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 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 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 avatar image

Related Questions

Problems with GameManagers 3 Answers

Trying to build system without hard coupling 2 Answers

In delegate function, add the same listener event, why the same listener be called Immediately? 0 Answers

EventManager with parameters 1 Answer

Subscribe to an event when the application is loaded 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