OnClick events in Unity

Daniel Kirwan
2 min readSep 22, 2021

--

Objective: Use the OnClick events so that the user can click the options in the shop.

I created a shop for my game and I wanted to have the item have an image that shows the user which item they have clicked on. I chose a blue background and created a gameobject out of it.

For a button click to be able to access the method you want to call it needs to be made public, otherwise it will not show in the list.

With your button selected click the plus icon on the button component to add a new OnClick event. Then in the empty slot drag in the gameobject that contains the script that has the method you want to call.

It will then allow you to select the method you want. Click the drop down arrow and then the script you want, then select the method you want to call. For me I want to call my SelectItem script in the ActivateShop script. I need to pass in a parameter for this method which is an int.

That is it for setting up an OnClick event in Unity. When the game is in play made it will run the code inside the method. In my case that is running a switch statement depending on the int passed in. I then update the UIManager with where the blue image should be on the UI.

--

--

Daniel Kirwan
Daniel Kirwan

No responses yet