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
2
Question by Xarbrough · Nov 20, 2015 at 01:15 PM · eventlistener

UnityEvent prevent listener from being added twice

I'm looking for a way to prevent a listener method being added twice to a Button onClick event.

Can I just check in the list of listeners, if it already exists and only add it if it doesn't or is there some other elegant way to prevent the same method being added multiple times?

I'd like this as a safety measure against designers who might add button clicked listeners through the inspector, although they are already set in code or vice versa.

Thanks!

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 Krishx007 · Mar 31, 2021 at 04:12 PM

I implemented this extension method but it's not working currectly. Could anyone help?

     public static void AddListenerOnce(this UnityEvent unityEvent, UnityAction unityAction)
     {
         for (int index = 0; index < unityEvent.GetPersistentEventCount(); index++)
         {
             Object curEventObj = unityEvent.GetPersistentTarget(index);
             string curEventName = unityEvent.GetPersistentMethodName(index);

             Debug.Log("curEventName: " + curEventName + ", unityAction: " + unityAction.Method.Name);

             if ((Object)unityAction.Target == curEventObj)
             {
                 Debug.LogError("Event is already added: " + curEventName);
                 return;
             }
         }

         unityEvent.AddListener(unityAction);
     }
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 unity_MXGApNcapNfNLg · Apr 01, 2021 at 07:14 AM 0
Share

What I usually do is i clear the event before assigning the new one instead of checking for the existing.

e.g in your case I will do this:

unityEvent=new UnityEvent(); unityEvent.AddListener(unityAction);

or we can use:

unityEvent.RemoveAllListeners();

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

35 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

Related Questions

listen for flash events 2 Answers

Assigning events on runtime 0 Answers

Persistent listeners for Button.onClick are removed at runtime, if buttons are instances of prefab 1 Answer

Listener for GUIText.text change? 0 Answers

Any way to pass the function of a listener as a string? 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