Buttons single click

I have a button and i want my function to do only one time when I press the button and I don’t want to use the inspector
public Button lvlUpButton;
private int priceOfLvlUp = 10;
bool lvlUpEndCheck = true;
void Update()
{
lvlUpButton.onClick.AddListener(testDebug);
}
void testDebug()
{
Debug.Log(“fu”);
}
if i press the button i get 1+ thousand times my function


Should I use another function instead of Update or I should use coroutine I think if I use coroutine if u press the button more than a time i will use in coroutine u will get the same result. Or i should make my button not interactable after it will do for a second. Sorry for my bad English if u didn’t get smth ask me please

Yes. You only need to assign a listener once, not every frame. Your application will endlessly consume memory assigning an infinite number of listeners until it runs out of memory & crashes.

Assign the listener once in a method like Start or Awake.

1 Like

Thank you very much