Search code examples
c#unity-game-engine

How to trigger the On Click function of a button in Unity?


I have a button with a click handler assigned to it. In my script I want to trigger that click function without the user clicking the button. How can I do this?

I figured with gameObject.GetComponent<Button>().onClick; it would work, but it doesn't.


Solution

  • This should work:

    gameObject.GetComponent<Button>().onClick.Invoke(); 
    

    UnityEvent.Invoke

    I dont even know what onClick does

    Go to the documentation:

    Button.onClick