How to Play Sound Effects in Unity

Niraj Karki
May 22, 2021

--

Day 30- Making 2D Space Shooter Game

Objective: Play audio clips from the script

Lets start by creating a empty gameobject in the hierarchy.

Here, we will be adding a music clip in our label_music gameobject and use it to play a clip whenever player fires a laser.

Add a Audio Source in your Laser_Music gameobject and drag your laser music clip in Audioclip. Be sure to deselect Play On Awake or else it will play right after you start the game.

Add a variable to store the destination of your clip.

Find the Laser_Music gameobject in which you have stored your music clip and the use GetComponent to access the AudioSource of that gameobject.

Finally play the audio clip whenever player shoots laser by adding it just after instantiating the laser.

--

--

No responses yet