How to Play Sound Effects in Unity
Music, SFX, Voices and more
Objective: Play a sound effect in Unity through script
First select a GameObject in the Scene and add the component AudioSource.
To make the basic setup you have to assign a clip in the AudioClip attribute, this is the audio file to play. Then you can check/uncheck the Play On Awake checkbox depending on the type of audio. You can check it for background music or environment sounds and you can uncheck it for the effect that you want to play at a specific moment of the application.
To play the sound in the script you have to get the audio source component and use the function Play. In this code snippet you can play the sound when the user presses the P key.
That’s it! Basic setup to play a sound in an Unity application.