How to add and play sound effects in Unity
To play a sound effect in Unity, import the file as an AudioClip, add an AudioSource to a GameObject, and call PlayOneShot with the clip from a script. The examples below are CC0 sounds from Uncommon Ear, which need no credit.
Last updated:
Example tracks
These are real catalog tracks. The credit line under each row is the one Uncommon Ear generates.
-
CreatorAssets UI.
0.3 s - 0.4 s
-
Qat Foley.
1.1 s -
OwlishMedia Foley.
0.8 s
Steps
Import the file
Drag the sound into your Assets folder, for example Assets/Audio/SFX. Select it and open the Inspector to see its import settings.
Choose a compression format
For short effects, choose PCM. The manual recommends PCM for short sound effects and higher quality, at the cost of larger files. If a sound is noisy and plays in large quantities, such as footsteps, impacts and weapons, choose ADPCM, which is about 3.5 times smaller than PCM. Choose Vorbis for medium-length effects and music. Select Apply when you finish.
Choose a load type
For small sounds, set Load Type to Decompress On Load, which avoids decompressing during gameplay. For a large file, avoid it, because the manual says Vorbis decompression then uses far more memory. If a file uses too much memory that way, choose Compressed In Memory. Turn on Force To Mono if the effect does not need stereo.
Add an AudioSource
Add an AudioSource component to a GameObject. If you start the sound from a script, turn off Play On Awake, because that setting plays the source as soon as the component or GameObject becomes active.
Play the sound with PlayOneShot
Use PlayOneShot for effects. It does not cancel clips already playing on the source, so rapid sounds overlap. The second argument scales the source volume. Unity clamps negative scales to zero, and scales above 1 might clip.
Play the sound with PlayOneShotusing UnityEngine; public class ButtonSound : MonoBehaviour { [SerializeField] private AudioSource source; [SerializeField] private AudioClip clickClip; public void OnClick() { source.PlayOneShot(clickClip, 0.8f); } }Play a sound at a position in the world
If the sound comes from a point in the scene and no object needs to own it, call PlayClipAtPoint. For a sound on a moving object, use an AudioSource on that object and set Spatial Blend, where 0 is fully 2D and 1 is fully 3D.
Play a sound at a position in the worldusing UnityEngine; public class Explosion : MonoBehaviour { [SerializeField] private AudioClip boomClip; private void Start() { AudioSource.PlayClipAtPoint(boomClip, transform.position); } }Route effects to a mixer group
To control all effects with one volume slider, create an Audio Mixer asset with a group for effects, then route the AudioSource output to that group. You can also do it from a script.
Route effects to a mixer groupusing UnityEngine; using UnityEngine.Audio; public class SfxRouting : MonoBehaviour { [SerializeField] private AudioSource source; [SerializeField] private AudioMixerGroup sfxGroup; private void Awake() { source.outputAudioMixerGroup = sfxGroup; } }
Common mistakes
- Importing a long music track with Decompress On Load. It decompresses the whole file into memory.
- Leaving Play On Awake on for an effect you trigger from a script. It plays once when the scene loads.
- Calling PlayClipAtPoint for a sound that fires many times a second. Each call creates a temporary audio source; PlayOneShot on an existing source avoids that.
- Using a volume scale above 1 to make a sound louder. It might clip. Raise the clip's level in an audio editor instead.
- Leaving Spatial Blend at 0 for a sound that belongs to an object in the world. 0 is fully 2D, so the sound ignores distance and position.
- Skipping the license check. CC0 sounds need no credit, but a CC BY sound needs its credit line in your game credits.