Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
0
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 this 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 something 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 nothing happens, but perhaps later on the listener will we spawned into the scene and now if you activate the trigger again something 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
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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

The idea behind 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 which (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 this by writing a bit of code in your class that has the event.

For example:

 // Add to and remove from this list with public methods
 private static readonly List<Action> responses = new List<Action>();
 
 public static TriggerSingleRandomResponse()
 {
   responses[Random.Range(0, responses.Count)]();
 }
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

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