- Home /
Stop audiosource clip from playing
Hi,
When a condition happens I play an audio clip but I also want it to stop on a certain condition and play another clip. The problem is that I do not know how to stop this clip from playing. Code:
if(i == 0){
i=1;
AudioSource.PlayClipAtPoint(breathing1, transform.position);
yield WaitForSeconds(2);
audio.Stop();
Debug.Log("stop sound");
}
Answer by AlucardJay · Nov 13, 2012 at 06:22 PM
Unfortunately, I don't think you can stop PlayClipAtPoint unless you find it as a gameObject and destroy it. Aldo has covered this topic here : http://answers.unity3d.com/questions/316575/adjust-properties-of-audiosource-created-with-play.html
Is there any reason you have to use PlayClipAtPoint and not a permanent audioSource that you just use audio.Stop(); and change the audioClip ?
Edit :
Create an empty GameObject > Create empty
Attach the Component Audio > Audio Source
un-tick Play on Awake
Attach the below script, then drop sounds in the Inspector.
press play
hit 1 or 2 . The audio should stop, change clip and play the selected sound.
Script :
#pragma strict
var clip1 : AudioClip;
var clip2 : AudioClip;
function Start()
{
audio.Stop(); // just incase PlayOnAwake is ticked
}
function Update()
{
if ( Input.GetKeyDown(KeyCode.Alpha1) )
{
audio.Stop();
audio.clip = clip1;
audio.Play();
}
if ( Input.GetKeyDown(KeyCode.Alpha2) )
{
audio.Stop();
audio.clip = clip2;
audio.Play();
}
}
Here is a video on audio source : http://www.unity3dstudent.com/2010/07/beginner-b10-audio-basics/
If you use this method, just make the empty gameObject with the audio as a child of your character, then everywhere the character goes, the sound follows. PlayClipAtPoint is usually to play a ricochet sound at the point the bullet hit an object, or other similar scenarios where you only want one sound at a position to play once only (then gets destroyed, pretty much the same as PlayOneShot).
Well it's a first person game and I want the sound to play at the character. Not that it plays and once I walk away the sound stays at that specific position. I cannot attach it to an object and destroy it because I want to be able to use these sounds more often. Furthermore I'm quite new to Unity and I am not sure what you mean with the permanent audioSource.
Check the edit of my answer, hopefully this and the video will clear up Audio Source.
also the Unity docs :
http://docs.unity3d.com/Documentation/Components/class-AudioSource.html
http://docs.unity3d.com/Documentation/$$anonymous$$anual/Sound.html
The unity3Dstudent site is very useful, and all the videos are under 5 $$anonymous$$utes each.
Here are some links I strongly suggest to all new users :
Start at the bottom and work up : http://www.unity3dstudent.com/category/modules/essential-skills/
Start at the bottom and work up : http://www.unity3dstudent.com/category/modules/beginner/
this is the YouTube link for the above as one playlist : http://www.youtube.com/watch?v=-oXYHNSmTxg&list=PL27B696FB515608D2&feature=plcp
the Unity Wiki : http://wiki.unity3d.com/index.php/Tutorials
A list of resources : http://answers.unity3d.com/questions/12321/how-can-i-start-learning-unity-fast-list-of-tutori.html
Helpful page with information on using Built-In Arrays and Lists :
The unity wiki link above is very handy with lots of scripts and shaders too (just check out all the links down the left, and the tabs along the top : http://wiki.unity3d.com/index.php/Scripts )
http://forum.unity3d.com/threads/132628-How-to-help-the-Absolute-Beginner