Playing Game Audio in your game

Yiu Chu
2 min readMay 31, 2021

--

The Audio File Formats that Unity supports include .aiff, .mp3, .ogg and .wav

Depending on the nature of your audio clips, there are a few common ways to bring them to your game.

Playing Background music:

Simply add an Audio Source Component to an appropriate game object, then associate the AudioClip with the background music file.

Make sure

  • Play On Awake is checked, so it starts playing when the game starts
  • Loop is checked, so the music will start again from the beginning when it reaches the end.

Playing Sound when some events happen to the game objects

Besides adding Audio Source Component and associate the AudioClip with the background sound file, you need to make sure

  • Play On Awake is not checked. The sound should only play when the event happens
  • In the object’s script, get a handle to the object’s Audio Source, then play the sound using the Play() method.

Playing Sound and Destroy the same game object within the same frame.

It is common to have a game object play some sound effect before destroying itself.

However, doing this in the same frame means we won’t be able to hear the sound as the AudioSource within the destroyed object is gone as well.

One way to solve this is to use PlayClipAtPoint method instead.

All you need is a reference to AudioClip.

An Audio Source will be automatically created to play the clip, then get removed once the clip finished playing.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Yiu Chu
Yiu Chu

Written by Yiu Chu

A senior software engineer determined to be one of the industry's best Unity developer in 2D, 3D and VR

No responses yet

Write a response