Simple background music player

Let’s go over a simple implementation of a background music player in order to improve the overall feeling of the game.

Start by adding a game object and call it “Audio_Manager”. Add another game object as a child and call this object “Background_Music”:

Add an AudioSource on the Background_Music object and add music clip to the AudioClip field. Leave Play On Awake and Loop enabled:

Create a new script called AudioManager and add to the Audio_Manager object. Open the script and add private AudioSource and a private AudioClip array:

Add the Background_Music object to the AudioSource field and add the music clips to the array:

Add a function called ChangeMusic. Set a random audio clip and call the Play function on AudioSource to start playing the audio clip:

If you add the above function as public, you will be able to call it from a UI button. For testing purposes we can add a check for input in the Update function:

When you play the game now the AudioClip you added to the AudioSource on the Background_Music object will start to play automatically.

Good Luck!



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