Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 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 Reddevildragg · Jun 29, 2015 at 10:44 AM · listenerguibutton

button remove listener

i have added a listener to a dynamically created gui button using the button.onClick.Addistener() function and that all works fine.

i am now trying to be able to remove listeners from the button but i am being unable to do so.

my code is:

 void callTest()
 {
   RemoveListener(button,() => {callTest();});
   Debug.Log("removing call from button");     
 }

 public static void RemoveListener(Button button,UnityEngine.Events.UnityAction call)
 {
     button.onClick.RemoveListener(call);
 }

The function is calling and then calling the static function, but then will not remove the listener as every time i click the button it recalls the initial function. it does work if i use button.onClick.RemoveAllListeners(); however this is not much help as it will remove all my listeners when i only need to remove one (only does something the first time, but continues to do the rest after) and don't want to re-add all the other functions every time it is clicked.

anyone have an idea why this will not be removing. useing the debugger shows no errors and that the correct function is being sent into the remove function

Thanks in advance

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 Hellium · Jun 29, 2015 at 10:51 AM

I advise you to take a look at the answer I gave to this question :

http://answers.unity3d.com/questions/993738/unityaction-vs-method.html

You will have to use delegates / UnityActions to keep a reference on the function you add to your listeners.

With the following line : RemoveListener(button,() => {callTest();}); , you try to remove a listener you are creating. In fact, () => {callTest();} creates a new delegate. Thus, it hasn't the same reference as the one you have added before.

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 Reddevildragg · Jun 29, 2015 at 12:44 PM 0
Share

Thats great. I did not realise that it was creating a new delegate which does explain why it was not working.

$$anonymous$$odified from that post to store the action in a dictionary with the function name as a string as the key for reference purpose and this seams to be able to let me now remove them correctly

Thanks for the assist

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to get the GUI buttons to render properley 0 Answers

How to reflect / bounce audio off of a 3D Object? 4 Answers

AddListener to a Toggle. 1 Answer

Change the material of a buttons in List and keep it by draging around the screen? 2 Answers

Problem Displaying gameName when refreshing masterserver 0 Answers


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