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 GetKeyUp instead of GetKeyDown? It could be rapidly toggling on and off.

–

I'll give it a go. Be right back.

–

Is there a way to make it toggle the mute when the button is not held down, just tapped?

–

That's what GetKeyUp should do. GetKeyDown 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 GetKeyUp 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 GetKeyUp and it seems like the audio will play whenever the button is held. I'm sure what's going wrong.

–

2 Answers

2

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!

Thank you so much for your help!

–

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;
}