How to stop an audiosource.

Updated on March 1, 2016 in [A] Audio
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
6 on February 28, 2016

Hello Guys, i want to stop an audio source.
I have a game and during the play i have an ambiental song.
When i die, i have another song ( and it respawns me )
But, the ambiental song continues to play and i can hear both of them.
I just want a way to stop the audio source ( it’s bound to an empty object ) and let play the game over song which is bound to my character.

  • Liked by
Reply
0 on February 28, 2016

Well, i think you can do
AudioSource.Stop ();

  • Liked by
Reply
Cancel
0 on February 28, 2016

Thank you for your help. It isn’t working, it’s giving me a compiler error, i already tried this. I found this : GetComponent.<AudioSource>().Stop(); , it doesn’t give me errors, but it isn’t working.. I think i will better give you the codes : 

The ambiental music is bound to an empty object, so i don’t use a code for it.
The game over sound : ” #pragma strict

var DistantaMaximaDeCadere = -10;
var Restart = false;

function Update ()
{
if (transform.position.y <= DistantaMaximaDeCadere)
{
if (Restart == false){

GameOverMuzica();}
}
}

function GameOverMuzica () {
Restart = true;
GetComponent.<AudioSource>().Play();
yield WaitForSeconds (GetComponent.<AudioSource>().clip.length);
Application.LoadLevel(“Level 01”);
}

____________________________________________________
DistantaMaximaDeCadere = The maximum distance of falling ( just to let you understand )

I tried adding a code on the empty object which has the ambiental music on it but it didn’t worked. The code is this : #pragma strict

var DistantaMaximaDeCadere = -10;

function Update ()
{
if (transform.position.y <= DistantaMaximaDeCadere)
{
GetComponent.<AudioSource>().Stop();
}
}

*That’s the error i get with your code : Assets/StopAmbient.js(11,29): BCE0020: An instance of type ‘UnityEngine.AudioSource’ is required to access non static member ‘Stop’. *

  • Liked by
Reply
Cancel
0 on February 28, 2016

You’re error is because the audiosource you want to use is on another gameobject.
It’s best if you were to create a reference to the audiosource in the inspector.

  • Liked by
Reply
Cancel
0 on February 28, 2016

The script for the audio source i want to stop it’s on the game source which has the audio source on it. And the script for playing the gameover sound is bound to the game object which has the audio source with the gameover sound on it.

  • Liked by
Reply
Cancel
0 on February 29, 2016

You should make a variable of type AudioSource. Have you already learned the differences between static and public variables? If so, Stop() is a public method, not static.

  • Liked by
Reply
Cancel
0 on March 1, 2016

I’ve done it After MILLION AND MILLION of hours of thinking :)) ( and youtube watching for better understanding unity’s sound engine )

I just made it on a code.

#pragma strict

var maxFallDistance = -10;
var Muzica : AudioSource;
var GameOverSound : AudioClip;
private var isRestarting = false;

function Start () {
Muzica.Play();
}

function Update ()
{
if (transform.position.y <= maxFallDistance)
{
if (isRestarting == false)
{
RestartLevel();
}
}
}

function RestartLevel () {
isRestarting = true;
GetComponent.<AudioSource>().clip = GameOverSound;
GetComponent.<AudioSource>().Play();
yield WaitForSeconds (GetComponent.<AudioSource>().clip.length);
Application.LoadLevel(“Level 01”);
}

  • Liked by
Reply
Cancel