How to Play Audio from a Script in Unity

Опубликовано: 28 Февраль 2025
на канале: Game Dev Beginner
23,687
531

Want to know how to CODE in Unity? Get my course here: https://www.udemy.com/course/how-to-c...

There are five main methods for playing audio and sounds from scripts in Unity. In this video you'll learn how each method works, the differences between them and the right way to use each one in your project.

00:00 The 5 different methods
00:13 Play
00:48 Play One Shot
02:20 Play Clip At Point
04:16 Play Delayed
04:54 Play Scheduled
05:59 Outro

Read the full guide here: https://gamedevbeginner.com/how-to-pl...

There are five main methods for playing music and sounds from scripts in Unity and which one you use depends on what you're trying to do. In this video, I explain how each method works, how to call it from a script and when you're most likely to find it useful:

Play()

Play is the most common method of triggering audio from a script in Unity, and will play whatever Audio Clip is entered into the Audio Clip field on the Audio Source. It's best used for looping sounds and music, this is because Play (unlike Play One Shot) supports looping sounds.

PlayOneShot(AudioClip clip, float volumeScale)

Play One Shot triggers a single sound and is ideal for sound effects. To use Play One Shot, you'll need to specify an Audio Clip to use and set an optional volume to play at. Using Play One Shot will allow you to play multiple sounds from an Audio Source at once (unlike Play which only plays one Audio Clip at a time).

PlayClipAtPoint(AudioClip clip, Vector3 position)

Play Clip At Point creates a temporary Audio Source and plays a single 3D sound once. This is great when you can't use an Audio Source component on an object but still want to play a sound from that position (for example, when you're destroying an object).

PlayDelayed(float delay)

Play Delayed works the same as Play except the start is delayed by a number of seconds. This is a really easy way to alter the timing of a sound or music. In this example I've used Play Delayed to sync a sound effect when a door swings shut.

PlayScheduled(double timeToPlay)

For precise timing, such as beatmatching audio and stitching clips together, Play Scheduled can trigger an Audio Clip in Unity extremely accurately. It's less simple than Play Delayed but is a must for precise audio timing (such as for dynamic audio systems).

Read my Ultimate Guide to Play Scheduled here: https://gamedevbeginner.com/ultimate-...

Links

Beginner game development tips: https://gamedevbeginner.com
Royalty Free Music for your games (Unity Asset Store): https://assetstore.unity.com/packages...
More about me: https://johnleonardfrench.com
Music: Bad Snacks "In the Atmosphere" -    / @badsnacks