- Home /
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;
}
}
}
Answer by DaiDesign · May 06, 2013 at 01:21 AM
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;
}
}
}
}
Answer by trs9556 · May 05, 2013 at 11:34 PM
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.
Your answer
Follow this Question
Related Questions
Muting Audio 2 Answers
Unity5, How to mute all sound? 2 Answers
Says Cant Play Sound Because AudioSource Is Disabled When Its Not 1 Answer
Using a Reverb Zone to create a zone of silence 1 Answer
Audiosource.Play() not working 0 Answers