UnityAction/UnityEvent - remove listener from within delegate
UnityEvent eventHandler = new UnityEvent();
UnityAction eventAction = new UnityAction(delegate() {
if(condition = true) //the condition doesn't matter, its being met
eventHandler.RemoveListener(eventAction); //parameter shows up red in monodevelop, as if it doesnt exist
});
eventHandler.AddListener(eventAction);
I understand that trying to remove an event action from within its constructor probably isn't possible, which is why it can't find eventAction. Is there some way i can access the object being constructed so i can specifically remove that listener at a later time or once a condition in the delegate is true? is there a special keyword for it? or is there a better way to handle a function like this?
Comment
Best Answer
Answer by Hellium · Apr 10, 2018 at 08:04 PM
As you have guessed, you can't use a variable while it is initialized. You need an additional step:
UnityEvent eventHandler = new UnityEvent();
UnityAction eventAction = null ;
eventAction = new UnityAction(delegate() {
if(condition == true)
eventHandler.RemoveListener(eventAction);
});
eventHandler.AddListener(eventAction);
Your answer
![](https://koobas.hobune.stream/wayback/20220612160456im_/https://answers.unity.com/themes/thub/images/avi.jpg)