Can't catch a button's On Click event

I’m a newbie and have completed the roller ball project and added some extras - so far so good. Experimenting with the UI but cannot get a button to call my function when clicked. To remove any distractions have created a new project, added a button, a script that writes to the console log and then called the script from the On Click. The button changes colour when clicked but does not call my script. Help! Two screen shots attached showing what I’ve done.


It looks like what you’ve done is drag your script to the On Click assignment box, when what you actually should be doing is dragging a Game Object from the scene there. That Game Object would have your clickButton script attached to it. Then, from the dropdown, you’ll be able to select clickButton.PressMe, which will run the function you’ve written.

It’s a bit confusing at first, but when you get the hang of it, it gives you a lot of flexibility to add functionality without having to write functions, even. For example, if I wanted a button to make an object disappear, I just drag that object to the button’s On Click, and call the GameObject.SetActive(false) function.

For custom functions, I’ve been creating a script called GUIEvents and attaching it to the EventSystem object that gets created automatically when you add a Canvas. I then assign that to buttons that need to do things not handled by other objects in the scene (like loading the Main Menu scene or something).

1 Like

Thanks Schneider21, that works as you said. The flexibility of linking scripts through objects is something I will watch out for.