Get code examples like 'unity button set onclick c#' instantly right from your google search results with the Grepper Chrome Extension. In this one, we learn how to use the OnClick event that the Button component gives us to call a function we've defined somewhere in our code using a real exa.
The onclick
attribute can be used in div
tags, form
buttons, img
image tags, a
link tags, and other HTML containers.
(HTML containers being HTML tags that can contain content. As examples, the br
tag isn't a container, but the td
tag is.)

Why use it? To make something happen when something is clicked. Ideas:

- Reveal content with a link click.
- Make a div disappear when it's clicked.
- Check an email address with a button click.
- Send the browser to a different URL when the content in a
span
tag is clicked.
Pretty much anything a web developer can come up with.
Here's an example onclick
attribute to spawn an alert box.
And here is how it might be used in a div
container.

And here is the working implementation of the above example code.
But what if you want to do more than one thing with a click?
That's what this article is about.
How to Do Many Things With One Click
How to do many things with one onclick attribute.
We'll use fictitious function names One(), Two(), and Three() to show how to implement the onclick
attribute for calling multiple functions.
There are two ways to do many things with one click:
Put all the function calls into the
onclick
attribute's value.Separate the function calls in the
onclick
attribute's value with a semi-colon character and optional space:With that method, any values the functions return will be ignored. Which is fine if you don't need a return value. An example would be functions (i) send you an email, (ii) make an Ajax request to update a log, and (iii) spawn an alert box with a thank-you message.
It's the simplest method if you don't need return values.Create a custom function to call the other functions.
The custom function calls the functions the
onclick
attribute shall call.This example contains a div with an onclick attribute that calls the
Custom()
function.Custom()
calls functions for theonclick
attribute and may process values returned from the functions it calls.The last line of the
Custom()
function that reads asreturn val;
causes the return of the value received from the last function that was called. West mountain radio driver. It is there in case the callingonclick
needs the value.A form button might need the value. Here is an example
input
tag for a form button that needs the returned value so it can return the value to the form.
Those are the two ways an onclick
attribute can cause more than one function to run — listing the functions in the onclick
value or calling a custom function to run them. The latter method allows a programmer to make use of function return values.
(This article first appeared in Possibilities newsletter.)
Will Bontrager

- Unity Tutorial
- Unity Useful Resources
- Selected Reading
In this chapter, we will earn how to insert UI elements into our scene and go about working with them.
Let us start off with a Button. To insert a button, right click in the Scene Hierarchy and go to Create → UI → Button. Download synaptics scsi & raid devices driver. If you do not have an existing Canvas and an EventSystem, Unity will automatically create one for you, and place the button inside the Canvas as well.
Remember that in Overlay rendering mode, which is the default mode, the size of the Canvas is independent of the size of the camera. You can test this by clicking on the Game tab.
If you play the scene, you will notice the button already has some standard functionality such as detecting when the mouse is hovering over it, and changing color when pressed.
A Button requires functionality to be actually useful in the UI. This functionality can be added through its properties.
Unity Mouse Click
Let us create a new script, and call it ButtonBehaviour.
We have made a simple method that logs how many times we have hit the button.
Note − This method has to be public; it will not be noticed by the Button’s functionality otherwise.
Let us create an empty GameObject and attach this script to it. We do this because a button will not do anything on its own; it only calls the specified method in its scripting.
Unity On Click Button

Unity On Click Load Scene
Now, go into the Button’s properties, and find the OnClick() property.
Unity Onclick No Function
Hit the + icon on the bottom tab, and a new entry should show up in the list.
Unity On Click Gameobject
This entry defines what object the button press acts on, and what function of that object’s script is called. Because of the event system used in the button press, you can trigger multiple functions simply by adding them to the list.
Drag and drop the empty GameObject, which contains the ButtonManager script we created, onto the None (Object) slot.
Navigate the No Function dropdown list, and look for our OnButtonPress method. (Remember that it can be named anything you want, OnButtonPress is simply a standardized naming convention.) You should find it in the ButtonBehaviour section.
If you play the game now, you can test the button and surely enough, the console prints out how many times you have pressed the button.
