- Home /
Unity new UI. Working with scripts.
Hey. I have a little problem here. I have 2 buttons. For example A and B. And if i press A button, it sends a listener to onClick. But i need to remove this listener when i press B button. Generally, onClick.AddListener() works perfectly, but i don't no how should i use onClick.RemoveListener(). I tried it but didnt work. Help me please. P.S. Please, don't offer to use RemoveAllListeners.
Comment
Best Answer
Answer by steakpinball · Feb 26, 2015 at 04:03 PM
RemoveListener removes the action you send to it from the event. You send the method itself. Notice the lack of parenthesis after the name of the method on lines 6 and 10.
public class Handler {
Button button;
// Call this where you need to add the listener
public void AddHandler() {
button.AddListener(HandleClick);
}
// Button B calls this
public void RemoveHandler() {
button.RemoveListener(HandleClick);
}
public void HandleClick() {
// Do stuff
}
}
I'm not sure if you can do this to remove a listener you set through the inspector.
Your answer
![](https://koobas.hobune.stream/wayback/20220613184412im_/https://answers.unity.com/themes/thub/images/avi.jpg)