am using different sounds in differnt scenes but problem is that i can’t stop all sounds using slider in menu it’s only stop menu sound or sound for scene but when my game over menu button appear i clicked on that then sound new apear as i am using DontDestroyOnLoad in Background script
public class BGSoundScript : MonoBehaviour {
// Use this for initialization
void Start () {
audioSrc = GetComponent();
}
//Play Global
private static BGSoundScript instance = null;
public static BGSoundScript Instance
{
get { return instance; }
}
void Awake()
{
if (instance != null && instance != this)
{
Destroy(this.gameObject);
return;
}
else
{
instance = this;
}
DontDestroyOnLoad(this.gameObject);
}
public void SetVolume(float vol)
{
musicVolume = vol;
}
//Play Gobal End
}
//i am using BGSoundScript.Instance.audioSrc.Play(); and //BGSoundScript.Instance.audioSrc.Pause(); in other script but that also not //work for me
If you are trying to make a volume slider on your main menu that affects all sounds in your game, you don’t need to use DontDestroyOnLoad(). Instead use an Audio Mixer to change your sound levels in all scenes.
-
Make a new Audio Mixer tab in the Editor
-
Create a new mixer call it ‘Master’
-
In the inspector, right click on the ‘Volume’ text, and click ‘Expose…to script’, then click on the dropdown that says ‘Exposed Parameters’ in the audio mixer and right click to rename it to ‘Master Volume’
-
On all your audio sources, set them to output to your audio mixer
-
Create a slider and set it’s min value to -80 and it’s max to 0. That is the range of dB for your audio mixer, so you won’t need to do any math to adjust the values
-
Create a new script and do something like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Audio;
using UnityEngine.UI;
public class Audio : MonoBehaviour {
public AudioMixer audioMixer;
public float masterValue;
public Slider masterVolume;
void OnEnable() {
audioMixer.GetFloat("MasterVolume", out masterValue);
masterVolume.value = masterValue;
}
public void MasterVolume() {
audioMixer.SetFloat("MasterVolume", masterVolume.value);
}
}
Finally, in the Inspector, drag in your Audio Mixer and Slider and enjoy!