Scratch To Unity: Sound

Beginner Scratch Tutorial Unity

This post provides a handy reference guide to go from Scratch’s “Sound” category of code blocks to C# with Unity.

First Things To Do After Arriving In Germany
Thursday December 20, 2018

Studying abroad in Germany is an exciting adventure, but the first days in the country are challenging for everyone who’s new in this area. There are a few things you must do before you finally start a new normal life in Germany. It is important for you to know what are these things well in […]

The post First Things To Do After Arriving In Germany appeared first on Dua xe moto.

Code Blocks
Click on the picture of any of these Scratch code blocks to see how to write similar code statements in C#.

German Grade Calculator
Tuesday May 07, 2019

#ipt_fsqm_form_wrap_57 .ipt-eform-content, #ipt_fsqm_form_wrap_57 .eform-ui-estimator { max-width: 980px; } Javascript is disabled Javascript is disabled on your browser. Please enable it in order to use this form. Loading German Grade Calculator Your Average Grade* Your average grade in your grading system 1 Highest grade on your grading system* Best Possible Grade Lowest passing grade on your […]

The post German Grade Calculator appeared first on Dua xe moto.

You program over time using a Coroutine. Unfortunately there is no “Wait For Sound” statement that is built-in, but you have a few ways to simulate something similar. Let’s assume you are able to obtain a reference to an “AudioSource” component which already is assigned the “AudioClip” you want to play.
You can begin the playback of the sound like this:


And you could yield based on the clip’s length:

Film & Game Releases: Week of May 17
Monday May 13, 2019

CHECK OUT OUR LIST OF MOVIE RELEASES FOR THE WEEK OF May 17, 2019! From John Wick: Chapter 3 to The Sun is also a Star, here is a schedule of the films and games coming out soon! Film Release Schedule A Dog’s Journey Synopsis from The story of one devoted dog who finds the […]

The post Film & Game Releases: Week of May 17 appeared first on Dua xe moto.

yield return new WaitForSeconds(audioSource.clip.length);

Or yield each frame that the audio source is still playing:

while (audioSource.isPlaying)
yield return null;

You can also use a “WaitWhile” command like this:

yield return new WaitWhile(() => audioSource.isPlaying);

Suppose you have a reference to an “AudioSource” component which will play the sound as well as the “AudioClip” that you want to have played. You can obtain them through code or through the Unity Inspector:

public AudioClip clip;
public AudioSource source;

You assign the clip to the source in code like this:

source.clip = clip;

You begin playing the sound like this:


If you have a convenient reference to all of your audio sources, then you could loop over them and call the “Pause” or “Stop” method on each:

AudioSource[] sources = GetAudioSources();
foreach (AudioSource source in sources)

But in many cases if you want to Stop all sounds, it might be simpler to modify the “AudioListener”:

AudioListener.pause = true; // Pause all sounds
AudioListener.pause = false; // Resume all sounds

var audioSource = GetComponent<AudioSource>();
audioSource.pitch += 0.1f;

var audioSource = GetComponent<AudioSource>();
audioSource.pitch = 1f;

There is no equivalent for this code block. You can obtain a similar result by simply setting all effect variables to their default values, whether they had been modified or not.

var audioSource = GetComponent<AudioSource>();
audioSource.pitch = 1f;
audioSource.panStereo = 0f;
// etc

var audioSource = GetComponent<AudioSource>();
audioSource.volume -= 0.1f;

var audioSource = GetComponent<AudioSource>();
audioSource.volume = 1f;

var audioSource = GetComponent<AudioSource>();
var volume = audioSource.volume;

This should cover all of the code blocks in Scratch’s “Sound” category. If you have any questions about this reference guide feel free to ask below.
If you find value in my blog, you can support its continued development by becoming my patron. Visit my Patreon page here. Thanks!