How to Play Sound Effects in Unity
Objective: Learn how to play Audio Clips from script in Unity
Click on the Player in the Hierarchy and add an Audio Source, assign the clip you want to play and set Play On Awake to false. Go to the Player script and initialize the Audio Clip and Audio Source as private serialized fields. Then, assign them in the Inspector. But, you should also null check in case the component can’t be found so the game doesn’t crash. In the script, play the _audioSource from the FireLaser method be calling _audioSource.Play(). Finally, in the Inspector, turn the volume of the Background music down so that you can hear the sound effects.