button.OnClick listener triggers twice

1.7k Views Asked by At

My play button in scene changes the current menu scene to the actual level scene.

I added the listener method in the form of a delegate:

playLevel.onClick.AddListener(delegate { LoadLevel();})

Most of the times it works perfectly fine, but sometimes when I click the button the game lags for a second and the button can be clicked again therefore opening the Level scene twice.

Does anyone have any idea on how to prevent this from happening? Thanks

1

There are 1 best solutions below

0
On

Sometimes:) ? Looks like your button have a multiple listeners with same method. Maybe this just happens you're click same button more than a one time in same game session.

Your Code: playLevel.onClick.AddListener(delegate { LoadLevel();})

This code delegates your method block for a listener when you clicks EVERYTIME. I think just try playLevel.onClick.RemoveAllListeners() or just remove a specific one (if you dont want remove all) with use playLevel.onClick.RemoveListener(-your actionCall-) before delegates a new one.