Applying Sound to Coin Collection

Our coins already have a script to handle their collection, so we can easily call our sound effect from there. We just need to set up the appropriate Component on our coin Prefabs first:

1. Select the coinBronze Prefab in the Project window.

2. Click the Add Component button and select Audio > Audio Source.

3. Drag the coinCollect_01 sound file from the _audio directory into the Audio Clip field of the Audio Source Component.

4. Repeat this for each of the other five coin Prefabs.

We need only one line of code to handle our audio call in the script, as shown in Listing 11.6.

Listing 11.6 Changes to the CoinPickup Script

public class CoinPickup : MonoBehaviour{  public int coinValue ...

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

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.