Adding sound effects

We will create another AudioSource to play all sound effects. The way in which we have the sound effect play will be handled through code instead of having it play instantly and continuously.

Now, within the Hierarchy, select Create | Audio | Audio Source. Position it at 0,0,0 and rename it Sound, as shown in the following screenshot. Since we don't want a sound to play the moment the game starts, deselect Play On Awake. Also drag and drop the KeepAround.cs script into the Inspector:

Adding sound effects

I chose to have this single audio source for my sound effects and to have it continue through each scene to make muting and unmuting the sound ...

Get Mastering Unity 2D Game Development - Second Edition 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.