Creating the Scripts

Next, we will need to create a few scripts to hook the player up to the HUD elements, so when we take damage or collect coins in the level, they will update accordingly.

Let’s first add a script to the hud_sprite display so that we can add to the total when we collect coins. This will be slightly different from anything we have learned yet, as we are counting up but displaying the results using sprites.

1. Create a new script and call it “CoinCounter.” Place it inside the GUI folder of our _scripts folder, just for organizational reasons.

2. With the script selected, drag it and attach it to the hud_sprite element in the Scene or Hierarchy.

3. Double-click to open it and adjust the contents to match Listing 10.5.

Listing ...

Get Learning 2D Game Development with Unity®: A Hands-On Guide to Game Creation now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.