- Home /
Method added on a button click with a delegate is being called twice.
Hi. The problem is as stated in the subject. I'm adding a method to a button with a simple gameObject.GetComponent().onClick.AddListener(delegate { Method(a); }); and it's being called twice. After some investigation, I've decided to add a Debug.Log call to find out the origin of the calls with Debug.Log(new System.Diagnostics.StackTrace().GetFrame(1).GetMethod().Name); and received two results as follows Invoke b__8
It feels like Invoke is what I actually need to happen, but I've never ever seen b__8 and googling got me nowhere. Any suggestion/explanation is appreciated :)
Are you sure you are not adding the delegate twice? Try to add this before adding the delegate: onClick.RemoveAllListeners();
or
onClick.RemoveListener(delegate { Method(a); })