Detecting mouse click on a canvas UI button?

I have a script inside of the button that’s on a canvas. How can I execute some code or call a function when it is clicked?

In the inspector, you can set the buttons OnClick to a specific void, for example

publib Void buttonClick()
{
  // do something
}

attach the script to a button or empty game object, assign the button onclick to it then select the void you want to excecute

Shown here

http://unity3d.com/learn/tutorials/modules/beginner/ui/ui-button?playlist=17111

You can also use the interfaces to manually do this.

http://docs.unity3d.com/Manual/SupportedEvents.html

1 Like

Thanks everyone. I’ll try to apply this tomorrow. :slight_smile: