• 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 GeoffreyFreudenberg · Apr 30 at 01:47 PM · buttonbuttonseventeventslisteners

Check if a listener has already been added to a button?

I've run into a bit of a snag with creating a button in the editor.

I need to dynamically add listeners to my button, but it just keeps adding the same 3 listeners over and over because there is nothing stopping it from adding the same ones on each Awake.

alt text

Here is my code...

         var button = gameObject.GetComponent<Button>();
             
         if (receiver1 != null)
         {
             var go1 = receiver1;
             UnityAction<GameObject> action1 = new UnityAction<GameObject>(this.OnClickEvent1);
              UnityEventTools.AddObjectPersistentListener<GameObject>(button.onClick, action1, go1);            
         }
             
         if (receiver2 != null)
         {
             var go2 = receiver2;
             UnityAction<GameObject> action2 = new UnityAction<GameObject>(this.OnClickEvent2);
              UnityEventTools.AddObjectPersistentListener<GameObject>(button.onClick, action2, go2);            
         }
             
         if (receiver3 != null)
         {
             var go3 = receiver3;
             UnityAction<GameObject> action3 = new UnityAction<GameObject>(this.OnClickEvent3);
              UnityEventTools.AddObjectPersistentListener<GameObject>(button.onClick, action3, go3);           
         }


Any help is appreciated!

werewrwerf.png (53.6 kB)
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
1
Best Answer

Answer by mchts · Apr 30 at 03:00 PM

Define your button, action1, action2 and action3 globally and move all of your add listener methods inside OnEnable() method (instead of Awake). And then add these lines to your script :

 void OnDisable(){
     UnityEventTools.RemovePersistentListener(button.onClick, action1);
     UnityEventTools.RemovePersistentListener(button.onClick, action2);
     UnityEventTools.RemovePersistentListener(button.onClick, action3);
 }

Now your listeners will be removed every time your scripts done playing!

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 GeoffreyFreudenberg · Apr 30 at 10:30 PM 0
Share

Thank you very much! :)

avatar image mchts GeoffreyFreudenberg · May 01 at 08:27 PM 0
Share

You're welcome

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

131 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

Related Questions

How to access the persistent listener gameobjects on a button? 1 Answer

I want the gameobject field of my persistent listeners to reference an object other than the one they are on. Is this possible? 1 Answer

Any way to pass the function of a listener as a string? 1 Answer

Where is my OnClick in the button script? 1 Answer

4.6 adding onClickListener to instatiated buttons on a scrollview and click first button then it contain last index value of delegate method 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges