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

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

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.