- Home /
How to Mute / Unmute audio after WaitForSeconds()
I want to mute another AudioSource on another gameObject while I play a soundEffect and once that soundEffect has finished playing the music will unmute. How would I script that?
Thanks for any help it will be really appreciated thanks.
Here's something I did with muting, but I cannot help with C# and yield.
http://answers.unity3d.com/questions/233420/musical-game-mute-and-unmute-trigger-soundsource.html
Answer by Seth-Bergman · Aug 28, 2012 at 02:55 PM
you can pause it or mute it: AudioSource.Pause & AudioSource.mute:
http://docs.unity3d.com/Documentation/ScriptReference/AudioSource.Pause.html
http://docs.unity3d.com/Documentation/ScriptReference/AudioSource-mute.html
EDIT:
here's an example:
(javascript)
var otherObject : GameObject;
function Start(){
otherObject = GameObject.Find("NameOfObject");
}
function PlaySound(){
otherObject.audio.mute = true;
audio.Play();
while(audio.isPlaying){
yield;
}
otherObject.audio.mute = false;
}
I think this should do it
(edit again) Oops, looks like you're using C#, so like this I think:
private GameObject otherObject;
void Start(){
otherObject = GameObject.Find("NameOfObject");
}
IEnumerator PlaySound(){
otherObject.audio.mute = true;
audio.Play();
while(audio.isPlaying){
yield return 0;
}
otherObject.audio.mute = false;
}
my C# is rusty, but I think this should work
I know how to do that but how would I unmute another audiosource on another gameObject after the sound effect has finished playing using the code 'yield WaitForSeconds(soundEffect.length)'.
I'm not too sure where to put 'yield' i'm currently putting it in the Void Update() but i know that it will not work in there so where would I put it?
Thanks.
added an example above.. this would be on the object playing the sound effect. Then all that's left is to call PlaySound() at the appropriate time..
Thanks for the quick reply but how does this work i can't seem to make this work?
Thanks anyway.
I have solve my question but thank you very much for helping me much appreciated. this is what I used to solve my issue:
// Javascript
function $$anonymous$$uteSound(){
Playlist1.mute = true;
while(audio.isPlaying){
yield WaitForSeconds(audio.clip.length);
Playlist1.audio.mute = false;
Playlist1.mute = false;
}
I had to use javascript because I know how to use the Yield function in that language but not so much in C#.
Thank You anyway. :D