Create sound for game in Unity

After finish the first tutorial in this series, you have known how to change Scene and add picture in the game. In this tutorial, gamemakerlab will guide how to create sound and background music for game.


Step 1: Import sound into Unity

Import a sound file by drag and drop from outside to the Project area of Unity (do the same like import image from the first tutorial).

After import successfully, a file will appear like the below picture.

To know what Audio file that Unity support, you can see it in this link: 

create sound for game in unity

Step 2: Add sound in the Scene

After finish the above step, we have a sound file to using, but it doesn't activate because this file isn't added to the Scene.

Be sure that you are selecting the SplashScene scene -> select the SplashSceneManager GameObject  -> Add Component -> Audio Source.

 create sound for game in unity

We can see a Audio Source Component be appeared. This Component has some fields which need to pay attention, which will be listed below:

  • AudioClip: place to store sound files.
  • Mute: mute sound, the sound file is still running.
  • Play On Awake: audio will automatically operate when the scene start.
  • Loop: audio will repeat after the end.
  • Volume: select big or small volume.

create sound for game in unity

To create the background music, we will set the audio automatically operate and repeat.

Drag and drop the sound file into AudioClip, check in Loop like the below picture.

create sound for game in unity

Press Play, we can see the background music automatically operate in the SplashScreen Scene. When the game jump to another Scene, the audio will automatically mute, beacuse we just add it in the Scene PlashScreen GameObject.

Step 3: Create sound effect

To create sound effect, like a sound when we click mouse, we need another sound file,import that file into Unity and also create an AudioSource like the above step.

But this time, we won't check in "Play On Awake" and "Loop", like the below picture. We can create many Component Audio in one GameObject.

create sound for game in unity

Open the SplashSceneManager c# file, add this code in it:

create sound for game in unity  

LoadScene will take place immediately, so it need to delay a period of time enough to here the sound effect.

We just create 2 public variable AudioSource in code, but we need to "explain" for Unity can understand "what are they".

Drag and drop the corresponding component AudioSource in each position like the below picture 

create sound for game in unity

Press Play, you will hear the sound effect plays out when you click change Scene.

Conglaturation! Now, you know how to create sound for game. Just follow the step and your will succeed. Hope you like tutorials of Gamemakerlab, thank you for your visit and see you in other tutorial.