How to activate function ONLY when button is released from parent script

So my original Code was:

    void Update()
    {
        buyButton.onClick.AddListener(buyItem);

    }

When I hold the button (for like half a second), nothing gets printed from the Debug.Log line in the buyItem Function. However, when I release it, hundreds of messeges get printed out, and it seems that there are more messeges the longer I hold.

How can I make it so that the function only activates when the button gets released?

Two things :

  • You’re registering your buyItem event on Update, so every frame. Register your event elsewhere like in the Start function of your MonoBehaviour. This way you’ll only register once.
  • You’re in UI Toolkit forum, a future (but still WIP) new UI system for Unity. You should go to this forum for info in the classic UGUI system (that you are using) : Unity Engine - Unity Discussions