Nerd For Tech
Published in

Nerd For Tech

OnClick Events in Unity

Now that we have our shop keeper in place and a working shop window, let’s get to working on having proper interaction between the player so that we can buy what is in the shop. To start, let’s get it so that our shop shows the player how many gems they have to know if they have enough to purchase the items.
To start, we are going to create a UIManager script to hold all the information for UI information. Once we have it attached to our canvas, we will start making links towards our player count:

We will then go into our shop script, as it is what interacts with the player upon entering the trigger zone, and get our gem count through there:

With this in place, let’s see how it looks in our game:

Now that we have our gold count properly updating to the players total, let’s get to work on having our various items purchasable through OnClick events.

Next, let’s get it so that we can have a unique event when we click each button. To do this, we are going to link our On Click() parameter with our buttons to signify a specific number:

From here, we are going to have it so that when we click that particular item, for testing purposes, it will give us a log of which one we clicked on:

Now that we have it so that we have each button creating it’s own action, we are now going to set up our selection highlight so that the player knows which option they have selected:

What we have done here is in the shop script, we created the logic to know which of the buttons was pushed, and what position we want our highlight to be at, which is controlled in the UIManager like so:

We can now go back into Unity and see if clicking on the specific button will result in the highlight going overtop the proper item:

Finally, let’s get it set up so that when we click to buy on an item, we are able to purchase it with the diamonds we have collected. First, we will need to add the logic within our SelectItem void to give a value to work with, and create a new void to give us the logic to know if we can purchase it or not:

As we can see, it will take away the diamonds from the player if we are able to purchase the item, and if we can’t it will just close the shop. From here, let’s test it out in the game:

With our shop now working, we can now look to build in our game HUD and start with some of the final touches to the game.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store