- Home /
Audio Source will not "unmute" after toggling?
Hello,
I've been working at this for 2 days and I've had some great help, but it seems like something isn't working right. I am using this script to toggle the mute parameter on an Audio Source:
// Mutes-Unmutes the sound of this transfor each time the user presses escape.
function Update() {
if(Input.GetKeyDown(KeyCode.Escape)) {
if(audio.mute)
audio.mute = false;
else
audio.mute = true;
}
}
This script works perfectly for muting the audio source, but it doesn't seem to unmute the audio. Basically, if I start the scene with the mute button ticked on the audio source, it will not unmute the audio source and toggle the mute button each time I press escape. The check-mark on the mute parameter stays ticked on the audio source and no audio plays. If anybody could please help me out on this one, I would really appreciate it! Trust me, I've spent a ton of time on this one, and I just can't figure it out! THANK YOU SO MUCH for your help!!
Did you try using Get$$anonymous$$eyUp ins$$anonymous$$d of Get$$anonymous$$eyDown? It could be rapidly toggling on and off.
Is there a way to make it toggle the mute when the button is not held down, just tapped?
That's what Get$$anonymous$$eyUp should do. Get$$anonymous$$eyDown will return true whenever the key is held down, so you'll be toggling it constantly whenever the escape key is held. If you use Get$$anonymous$$eyUp it won't return true until the key is released, so that should work if you want to have it toggle when the button is tapped.
I changed it to Get$$anonymous$$eyUp and it seems like the audio will play whenever the button is held. I'm sure what's going wrong.
Answer by TargonStudios · Nov 20, 2013 at 05:09 PM
This script i made is rather crude. Currently, i can't test any script myself so i made it a simple one for you.
Replace if(audio.mute) and beyond with:
if(audio.mute == false){
MuteAudio();
}else{
Unmute();
}
function MuteAudio(){
yield WaitForSeconds(0.01);
audio.mute = true;
}
function UnmuteAudio(){
yield WaitForSeconds(0.01);
audio.mute = false;
}
Hopfully the script works. Hope this helps!
Answer by algaib · Aug 06, 2017 at 07:52 PM
Add this script below to the audio source, then drag the audio source itself into the public variable on the script component, then drag the audio source into the 'On Click()' part of the button. The button will then toggle the 'mute' button on the audio source.
public AudioSource appMusic;
public void MusicMute()
{
appMusic.mute = !appMusic.mute;
}
Your answer
Follow this Question
Related Questions
Pausing and playing audio on key press? 1 Answer
Can anyone help me invert this simple script? 2 Answers
how do i play a sound if i press a particular key? 2 Answers
Music will not change when an event triggers it 3 Answers
Stream music in Unity mobile 0 Answers