O'Reilly logo

Learning 2D Game Development with Unity®: A Hands-On Guide to Game Creation by James A. Henley, Matthew Johnson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required