I have this guy
map.OnCountryClick
+= (int countryIndex, int regionIndex) => ChosenCountry(map.countries[countryIndex].name, countryIndex);
and I want to remove it
// doesn’t work
map.OnCountryClick -= (int countryIndex, int regionIndex) => ChosenCountry(map.countries[countryIndex].name, countryIndex);
//doesn;t compile
map.OnCountryClick -= ChosenCountry;
1.) Paste the script you’re working with.
2.) Paste the compile error…
ChosenCountry does not match the signature of an OnCountryClick eventhandler. That’s why you are wrapping the method call with a lambda expression. When you remove the event handler you must remove the same delegate that you subscribed.
var handler = (int countryIndex, int regionIndex) => ChosenCountry(map.countries[countryIndex].name, countryIndex);
map.OnCountryClick += handler;
// Later do
map.OnCountryClick -= handler;
Your problem is that you subscribe to this event with an anynomous lambda expression. Because you have no delegate referencing it you cannot unsubscribe the exact same lambda expression again. Currently you just unsub a newly created anonymous delegate, which is a completely different instance than the delegate you want to remove.
So as Trexug has shown you need to store a reference to the new lambda expression in a variable and use it on the event.
Ahh cheers.That makes sense - Thanks for your help. I’ll give it a go when I get home tonight