Can anyone help me invert this simple script?

Good Afternoon,

I am currently attempting to work with music. In my scene, I have a Player and an Empty GameObject with an audio source attached to it. The audio source has the following script attached to it:

	function Update() {
		 if(Input.GetKeyDown(KeyCode.Escape)) {
		 	if(audio.mute)
				audio.mute = false;
			else
				audio.mute = true;
		}
	}

This script works perfectly, but the opposite way I want it to. The music from the audio source begins to play right when I start the scene and toggles on and off whenever I hit the Escape Key. I need the audio to not start UNTIL I hit the Escape Key and then toggle on and off after that. If anybody could help me out with modifying this script, I would really appreciate!! Thank you so much!!

Btw if you use audio.mute, the song will continue playing even if it's muted. So even if you set audio.mute to true at the beginning, the audio will still start playing, you just won't hear it. If you want to actually pause and continue playing the audio use audio.isPlaying, audio.Play and audio.Pause

2 Answers

2

does audio.mute = true; set the audio to mute? which is what i assume it does.

have you tried setting audio.mute in the start or awake functions?

function Start() {
audio.mute = true;
}

if that doesnt work you could also try

function Update() {

audio.mute = true;

if(Input.GetKeyDown(KeyCode.Escape)) {
if(audio.mute)
audio.mute = false;
else
audio.mute = true;
}
}

Thanks so much for your help! I'll go give that a try really quick.

You can also just check the checkbox behind 'mute' on the audio source component. This will start the scene with audio muted.

I just gave that second script a try and it didn't seem to work. It did check the mute box, but it didn't "unmute" when I pressed escape. The script I currently have does not "mute" the audio source. I tried look for the parameter it is adjusting and I can't seem to find it. Any ideas?

It tried to start the scene with the mute button pressed and that didn't work either. I just don't understand what's going on. It should be working.

can you please explain a bit further on where exactly and how exactly you are planing on using this script?

Just initialize the mute as true, and then whenever escape is pressed, switch it:

function Start() {
    aduio.mute = true;
}

function Update() {
    if(Input.GetKeyDown(KeyCode.Escape)) {
        audio.mute = !audio.mute;
    }
}