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
1
Question by GeoffreyFreudenberg · Apr 30, 2019 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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by mchts · Apr 30, 2019 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, 2019 at 10:30 PM 0
Share

Thank you very much! :)

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

You're welcome

avatar image
0

Answer by x1alphaz1 · Feb 26 at 07:01 AM

this code with give u the number of listerners added

 Button btn;
 Debug.log(btn.onClick.GetPersistentEventCount());

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

130 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

Related Questions

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

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

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

OnClick() problem with Button script: "Deleting an array element will copy the complete array to all..." 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