Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 Inok · Jul 06, 2015 at 08:26 AM · eventinstancedelegate

Prevent fire event from all instances.

This is my event manager:

 public class Event_Mgr
     {
       public delegate void Power_State();
       public static event Power_State On_Power_Enabled;
       public static void Power_Enabling()
       {
         if (On_Power_Enabled != null) On_Power_Enabled();
       }
     }

In script "Systems_Controller" at start method i write: Event_Mgr.Power_Enabling(); this fire event. In script "Movement" that subscribed to event i write:

 void OnEnable()
 {
   Event_Mgr.On_Power_Enabled += Enable_Movement;
 }
 void OnDisable()
 {
   Event_Mgr.On_Power_Enabled -= Enable_Movement;
 }

I have for example 2 instances of gameobject and each of them contain both scripts: "Systems_Controller" and "Movement". So as result i see that event fire twice for each instance. Question how make event to be fired only for same instance, not to all instances that subscribed to that event. Maybe event not best option for this? I have player wehicle and enemy wehicle so both need to enable power before they can move. But when i enable power on player, event triggered for both player and enemy. Then when enemy enable power event triggered also for both player and enemy.

Comment
Add comment · 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 fafase · Jul 06, 2015 at 08:31 AM 0
Share
 "Quetion how make event to be fired only for same instance, not to all instances that subscribed to that event"

This is not clear, do you want Script A and B to be called only on object A or object A and B to receive the call but only for one script?

avatar image Inok · Jul 06, 2015 at 08:41 AM 0
Share

no, problem that event "static" by its nature so this lead to trigger event on all instances that subscribed to event. I have player wehicle and enemy wehicle so both need to enable power before they can move. But when i enable power on player, event triggered for both player and enemy. Then when enemy enable power event triggered also for both player and enemy because event "static".

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by fafase · Jul 06, 2015 at 08:46 AM

Ok so first off the reason why it calls everyone is not due to the static, it is due to the fact that an event calls all listeners, static is different topic.

Since, your player and enemy are registered, they get called. If you nee the event to select the listeners, two ways, first most likely, you should have two listeners, one for player and one for enemy (or other items).

Second way, not so good, pass a parameter to your event to define what should happen.

 On_Power_Enable(ObjectType.Player);

 void On_Power_Enable_Listener(ObjectType typeObject){
      if(typeObject != myType){return;}
 }

But as you can see it requires extra computation that may lead to error or unwanted behaviour later on.

Comment
Add comment · Show 1 · 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 Inok · Jul 06, 2015 at 08:55 AM 0
Share

I think first method not an option because i can have more that 1 enemy at same time, so problem will not spread only on player. Second method looks not bad, because i not have alternative yet. Thanks anyway fafase

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

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

C# Delegates, a question 1 Answer

Sharing delegate types across scripts 1 Answer

What happens with the receiver when the same event is trigger from multiple instances at the same time? 0 Answers

Destroy(gameObject) only works once 1 Answer

Create delegate from MethodInfo 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