Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 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 Alverik · Aug 26, 2016 at 01:16 AM · c#eventssubscriptionlisteners

How to subscribe method to OnDisable in a gameObject.

Hi, I've just started using events and delegates, I know how to subscribe to some events in UI buttons/elements. But how do you subscribe or un-subscribe a method to the OnEnable/OnDisable events in a GameObejct? (if that's possible?).

I'm basically making a little prompt system with different possible types of prompts (which is using functionality from a third party asset-not explicitly made for that).

Anyway, I need to do add the method from an external script because I'm not always going to be doing this to the same menu and I'm not always going to be passing the same method. Also, at times the menus are going to be shared - one object will have to wait until the menu is free to be used (a limitation in the third party asset... the asset doesn't have a method to instantiate it's own menus, because the core asset doesn't need it...).

Sadly, I'm still too green to replicate the features provided by the asset, and if possible I'd like to not have to attach anything extra to the menus to make setup always easy. Hence I want to use events... And I need to trigger some random method when one of the prompts closes (I'm currently using a coroutine with wait time to detect it)...

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
2
Best Answer

Answer by edwingohbh88 · Aug 26, 2016 at 04:47 AM

You can take a look in this link, can use delegates to subscribe and unsubscribe in OnEnable(), OnDisable() methods respectively.

http://www.theappguruz.com/blog/using-delegates-and-events-in-unity

Alternatively, if you want to have a custom delegate to cater for all sorts of event, you can create a custom class to hold the delegate

 // class to hold the delegates
     namespace MyPackage
      {
      public delegate void CustomAction(object[] param);
      }
 
 // class to run it
  public class TestClass : MonoBehaviour 
  {
        public static CustomAction OnActionClicked;
 
      void OnEnable()
      {
                TestClass.OnActionClicked += HandleOnClick;
      }
  
      void OnDisable()
      {
              TestClass.OnActionClicked -= HandleOnClick; 
      }
 }

For full source, you can take a look at my GitHub, i've written some code to it. https://github.com/gohbiscuit/UnityCustomCoroutine/tree/master/UnityCoroutine

Hope it helps.

Comment
Add comment · Show 3 · 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 Alverik · Aug 26, 2016 at 08:36 PM 0
Share

Thanks, I'll accept the answer, but I haven tried it yet (will see later today). But if you don't $$anonymous$$d, an example of subscribing directly to OnEnable and Ondisabled would've been great... I had tried to get the GameObject from the menus and see if I could add a function/delegate to it but when I do I don't see the option appear like it does for OnClick. And If I try to just write OnDisable anyway I get a red warning. Is that somewhere else and not in the game object? Do I have to address monobehaviour or something? Or Are this events not native in the object like the onclick is in buttons? Do I have to be forced put a script on the menu with blank OnDisable function?

avatar image edwingohbh88 · Aug 27, 2016 at 05:26 PM 0
Share

Hi Alverik, I am not quite sure what u mean but if you mean something like triggered an event when the button is enabled or disabled you can do something like.

 // class to hold the delegates
 namespace $$anonymous$$yPackage
 {
     public delegate void CustomAction(object[] param);
 }
 
 // another class
 public class $$anonymous$$enuClass
 {
 
     // any na$$anonymous$$g you like, because is a custom delegate u defined in your namespace
     public static CustomAction OnButtonEnable;
     public Button testButton;
 
     void EnableButtonTest()
     {
         testButton.setActive(true);
 
 
         // I want to send a message to tell every class listening to the event that I am active
         // always check for not null when invoke the event
         if(OnButtonEnable != null) 
             // since it takes in an object[] param, you can pass parameters to it
             OnButtonEnable(true);
     }
 
 
     void DisableButtonTest()
     {
         testButton.setActive(false);
         // I want to send a message to tell every class listening to the event that I am inactive
         if(OnButtonEnable != null) 
             OnButtonEnable(false);
     }
 }
 
 
 // Class Listening to the event
 public class AnyClass : $$anonymous$$onoBehaviour
 {
     void OnEnable()
     {
         // subscribing for "ButtonClass" OnButtonEnable event, 
         // when class is active, because it is static you do not need to create a instance of that class.
         ButtonClass.OnButtonEnable += HandleButtonEnable;
     }
 
     void OnDisable()
     {
         // subscribe when class is inactive
         ButtonClass.OnButtonEnable -= HandleButtonEnable;
     }
 
 
     void HandleButtonEnable(object[] param) 
     {  
         //button enable
         if((bool) param[0] == true)
         {...} 
         // button disabled
         else
         {...}
     }
 }

This might be a little complicated but i hope it helps, u can look into my github, there are more examples there you can use.

avatar image Alverik edwingohbh88 · Aug 27, 2016 at 08:24 PM 0
Share

Thanks for the example, I'll take my time to read it. Anyways, sorry I wasn't been clear, but I meant to subscribe to the onDisable and onEnable on the UI Canvas. What I was wondering, was if you had to have a script with a declared OnEnable and OnDisable on the canvas to be able to access it remotely (from another script). Or if it was natively accessible? (sorry I'm new to unity and program$$anonymous$$g so I'm not sure how things work yet. $$anonymous$$y knowledge is like swiss cheese right now).

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

212 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 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

Multiple Cars not working 1 Answer

Unity Events, Listeners, Delegates, etc. 1 Answer

Distribute terrain in zones 3 Answers

C# event does not work 1 Answer

Does calling Destroy() on a GameObject/MonoBehavior drop references to any event listeners? 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