Setting Up Audio In Unity

Today’s Objective: Set up my game to play audio music and sound effects during game-play.

Unity AudioSource documentation

Audio for continuous music:

Create an empty GameObject and add an AudioSource component
Choose the music audio clip, and set the Source to loop and play on Awake

Now this AudioSource will play the chosen music clip on loop as soon as this scene is loaded. There are many improvements you could make to the process, but this is the simplest way to achieve the goal.

Audio for Sound Effects in game-play:

Create/Add another AudioSource to a different GameObject (for example, the player). Leave the AudioClip variable empty, and PlayOnAwake and Loop set to False.

Create a reference variable for the AudioSource and any clips you need to play.

In a script of your choosing, call a Play method using a reference to the AudioSource.

Play() is useful for sources where there will only ever be 1 clip played multiple times. PlayOneShot() is used when a number of different clips are being played at different times.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Vincent Taylor

Vincent Taylor

Unity game developer / C# Programmer / Gamer. Australian (Tasmanian) indie games developer for 10+ years. Currently looking for games industry employment.