How to play MUSIC and SOUND EFFECTS in Unity

Introduction

In this arti­cle we will see how to play music and sound effects in Uni­ty, mak­ing them play auto­mat­i­cal­ly when start­ing the game mode, but we will also see how to cre­ate a con­trol script to play music or sounds in Uni­ty through code.

To play audio clips in Uni­ty we use a com­po­nent called AudioSource, this com­po­nent allows us to con­fig­ure dif­fer­ent para­me­ters such as vol­ume, make it play in loop, among others.

The AudioSource com­po­nent by itself is not enough to lis­ten to the audio in Uni­ty, it is also nec­es­sary to have an Audi­oLis­ten­er com­po­nent in the scene, this com­po­nent is usu­al­ly assigned to the cam­era by default when cre­at­ing a new scene, the Audi­oLis­ten­er acts as the "ears" that hear the sound played by the AudioSources. We must make sure that there is only one Audi­oLis­ten­er com­po­nent in the scene, espe­cial­ly if we are using 3D sound effects, that is to say that we take into account the posi­tion from where the sound is orig­i­nat­ing, for exam­ple if a sound is pro­duced to the right of the player's per­spec­tive, that the sound is heard with greater vol­ume in the speak­ers on the right.

Dear read­er

🍷🧐

How to play an AUDIO CLIP in Unity at startup

1. Cre­ate a GameOb­ject and in the inspec­tor assign the AudioSource com­po­nent (Add Com­po­nent button).

2. Con­fig­ure the AudioSource com­po­nent with the "Play On Awake" option acti­vat­ed and the rest of the para­me­ters con­fig­ured as you like (see below for the dif­fer­ent options).

3. When you enter the game mode, the sound will start play­ing auto­mat­i­cal­ly with the para­me­ters set.

How to play an AudioClip in Unity through code

1. Cre­ate a GameOb­ject and in the inspec­tor assign the AudioSource com­po­nent (Add Com­po­nent but­ton). Deac­ti­vate the "Play on Awake" option and con­fig­ure the oth­er para­me­ters as you wish.

2. Cre­ate a Script to con­trol the start of the sound, the vol­ume, the end, among oth­er actions. Assign this Script to the same GameOb­ject that has the AudioSource component. 

3. In the script define a ref­er­ence for the AudioSource object and find it in the Start method using GetComponent<>().

4. Define a pub­lic func­tion to play the sound from any oth­er script, with­in this func­tion exe­cute the instruc­tion: "audioSource.Play()".

5. Detect the prop­er event in which the sound should be played and exe­cute the play func­tion defined in the audio script.

AudioSource component setup

The AudioSource com­po­nent has many adjustable para­me­ters to achieve dif­fer­ent results with our sounds. In the fol­low­ing image we see what the AudioSource com­po­nent looks like in the inspec­tor in Unity.

audiosource component to reproduce sounds in Unity
Fig. 1: Gener­ic AudioSource com­po­nent in Unity

To start with, you can man­u­al­ly assign the sound you want to play by drag­ging it to the Audio­Clip field. This can also be done from a script with the instruc­tion "audioSource.clip=aClip;", being "audioSource" the ref­er­ence of the AudioSource com­po­nent of the inspec­tor and "aClip" a field that con­tains the ref­er­ence of some sound.

Then we can mute the sound and adjust the vol­ume. Enable the "Play On Awake" option so that the sound is played as soon as the GameOb­ject enters the enabled state. The Loop option makes the sound play again when it is finished. 

The Spa­tial Blend is a para­me­ter that allows us to adjust if it is a back­ground sound or a sound that depends on the dis­tance between the AudioSource and the Audi­oLis­ten­er. If we put the indi­ca­tor in 3D, we will obtain a stereo sound whose vol­ume and dis­tri­b­u­tion in the head­phones will depend on the posi­tion of the source. With­in the 3D Sound Set­tings you can adjust para­me­ters relat­ed to this.

MOST SEARCHED VIDEOS FROM MY CHANNEL

ABOUT UNITY

ABOUT BLENDER

Scroll to Top
Secured By miniOrange