O'Reilly logo

Mastering Unity 2D Game Development - Second Edition by Simon Jackson, Ashley Godbold

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

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

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