My code looks like this:
public void ThrowAConfirmScreen(string msg, Action func)
{
confirmField.SetActive(true);
confirmText.text = msg;
confirmButton.onClick.AddListener(() => {
func();
confirmField.SetActive(false);
});
cancelButton.onClick.AddListener(() => {
confirmField.SetActive(false);
});
}
I tried making confirmation window by following a Youtube tutorial and the confirmation window actually works, but if you press cancelButton for a few time before confirmButton the func action executes as many times as cancelButton was pressed before. For example if I pass the func action Debug.Log("aa"); after canceling confirm window (pressing cancelButton) for 4 times and then pressing confirm I would get this:
I can't figure out why this behavior occurs and what causes it, can you help me?
I've already tried passing action as arrow functions, as separately declared function, passing Func<> instead of Action.

ThrowAConfirmScreenmultiple times. You are adding a listener to theonClickevent ofconfirmButtoneach time you call that method so if you call it twice then you'll add two listeners, so clickingconfirmButtononce would causefuncto be executed twice.