Unity Guide

OnClick Events | Unity

A quick guide about how to work with OnClick events with Unity

The shop

To start, let’s take a look at the current shop of our 2D game. Currently, we have 3 items on sale:

New input system

Also, as we’re using the “new” input system from Unity, we’ll need to replace the Standalone input module component from the EventSystem gameobject that gets created every time that we create a Canvas in our scenes. This way, we’ll be able to click on each button without trouble.

Creating the shop mechanic

And now, let’s start by creating the shop mechanics within a new C# script:

Using the OnClick events

And now, in order to display the respective information when we click the buttons of the shop, let’s use the OnClick events from the UI buttons to call the respective method in the Shop class.

  • Drag the shop gameobject (with the Shop class component) to the OnClick event.
  • Select the method we created when the button gets selected.
  • Drag the respective scriptable object into the OnClick event to be sent as a parameter.

Nerd For Tech

From Confusion to Clarification

Nerd For Tech

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/.

Fernando Alcantara Santana

Written by

A passionate computer technology engineer and Unity developer that is always looking to grow in every aspect of life :).

Nerd For Tech

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/.