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.

Intro To Unity ECS
Wednesday November 21, 2018

Unity’s ECS architecture is nearing its official release, and I am getting more and more excited for it. After a brief introduction, we’ll spend some time digging in and experimenting. By the end you should have an elementary grasp of Entities, Components and Systems within this new architecture. What is Unity’s ECS ECS stands for […]

The post Intro To Unity ECS 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#.

Masters in Data Science in Germany – Best Programs for 2019
Thursday February 28, 2019

Considered as the profession of the future, data science is a worth-seeking career, although pretty challenging. In this technology age, we’re living today, the need for professionals who are able to work out large sets of complex statistical data is ever-growing. What is Data Science? Data Science is an interdisciplinary field that combines a set […]

The post Masters in Data Science in Germany – Best Programs for 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:

What It’s Like To Write For A Network TV Show
Tuesday May 07, 2019

We chatted with the writer/creator of “F is For Family” and Co-producer of “The Simpsons,” Michael Price. If you can believe it, The Simpsons has been on Fox Television for 30 years and is the longest-running American sitcom, ever. Fox announced earlier this year that it has renewed the show for another two seasons, which […]

The post What It’s Like To Write For A Network TV Show 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!