Let’s talk about all the ways you can play and manage audio in your game. Audio makes an interesting topic; it’s relatively easy to get some audio playing in your game, but it’s often unclear how to move from the simplest use case to a slightly more complex one.
This is Unity for Software Engineers, a series for those seeking an accelerated introduction to game development in Unity.
AudioClip, AudioSource, and AudioListener
Let’s start with the three primary constructs involved with playing audio:
- An Audio Clip (in code
AudioClip
) is an asset representing a sound file. - An Audio Source (in code
AudioSource
) is a component on a GameObject. An Audio Source can play Audio Clips at the object’s location for any listeners to hear. - An Audio Listener (in code,
AudioListener
) is a component on a GameObject representing the object receiving (i.e., listening to) audio.
There should be one Audio Listener per scene. Typically, the Audio Listener component will be attached to the Main Camera object by default. You have full control over changing that, but putting your Audio Listener on…