OnClick Method not working.

Hey guys.
I’m quite new and I wonder, why my onClick method doesn’t work.
Only drag and drop version works…

public void OnClick()
{
Debug.Log(“test”);
}

Thanks a lot.

OnClick by itself does nothing. It’s just another method. If you want to tie it in by code, you need to add a listener to the onclick call.

Public Button button;

void Start()
{
   button.onClick.AddListener(clickMethod);
}

void clickMethod()
{
   //Stuff that happens on button click.
}

Just as a quick example. Basically telling it that when the button is clicked, it should call a method.

1 Like

You can also link it from the inspector directly, a little less complex than adding a listener manually.