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.

Scratch To Unity: Motion
Thursday July 25, 2019

This post provides a handy reference guide to go from Scratch’s “Motion” category of code blocks to C# with Unity. Code Alternatives Before you dive in, I wanted the opportunity to present a few alternatives for your animation needs. Unity has a lot of features you wont find in Scratch, so doing things the “Scratch […]

The post Scratch To Unity: Motion 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#.

Top 10 German Universities for International Students in 2019
Thursday February 14, 2019

Germany is among the top ten most popular study destinations in the world. According to the official data provided by the German federal statistical office, around 480,000 international students are studying in Germany. With the same healthy growth of international enrollment likely to continue in the upcoming years, Germany will soon climb as one of […]

The post Top 10 German Universities for International Students in 2019 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:

audioSource.Play();

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

5 Cheapest Cities to Live in Germany
Thursday March 07, 2019

The cost of living in Germany is quite affordable. But to you as a student every single cent matters and it is normal if you aim to save as much as you can. To a large extent, the place you choose to reside in Germany will indicate a large part of your spendings, thus it […]

The post 5 Cheapest Cities to Live in Germany 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:

source.Play();

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)
{
source.Stop();
}

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;

Summary
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!