Disable all sounds in game

Hi there,

I’ve got this script that I’ve modified from NGUI (UIButtonSound.cs) (I am in noway a coder) and I’m trying to disable all audio when a NGUI button is clicked.

I’ve used a the same script below, except I’ve used it for an audio source to disable music on a different NGUI button and that works.

Can someone please explain why the script below disables the audio listener but not re-enabled it.

using UnityEngine;
using System.Collections;

public class GameAudio : MonoBehaviour
{
	public enum Trigger
	{
		OnClick
	}
	
	public Trigger trigger = Trigger.OnClick;
	
	void OnClick ()
	{
		if (enabled && trigger == Trigger.OnClick)
		{
			AudioListener.volume = 0.0f;
		}
		else
		{
			AudioListener.volume = 1.0f;
		}
	}
}

Changed the code to this and it seems to work.

using UnityEngine;
using System.Collections;

public class GameAudio : MonoBehaviour
{
	public enum Trigger
	{
		OnClick
	}
	
	public Trigger trigger = Trigger.OnClick;
	private bool muted = false;
	
	void OnClick ()
	{		
		if (enabled && trigger == Trigger.OnClick)
		{
			if (!muted)
			{
				AudioListener.volume = 0.0f;
				muted = true;
			}
			else
			{
				AudioListener.volume = 1.0f;
				muted = false;
			}
		}
	}
}

Trigger.OnClick never changes therefore your if statement ALWAYS returns true, so it always puts the volume at 0. Add another enum for Trigger, and then have something change trigger to anything but OnClick.