- Home /
How to mute/unmute audio from a single scene while its being played in multiple scenes?
Hello, I have an "Toggle" to mute or unmute audio in my menu scene.It works fine, but when I go to Game scene and come back to menu scene the Toggle stops working.I have a script attached to the gameobject which allows the audio to play through all scenes without starting from beginning.And the same gameobject's values are used to Toggle. Please help me solve this issue. The script is....
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class MusicSc : MonoBehaviour {
// Use this for initialization
void Start () {
}
//Play Global
private static MusicSc instance = null;
public static MusicSc Instance
{
get { return instance; }
}
void Awake()
{
if (instance != null && instance != this)
{
Destroy(this.gameObject);
return;
}
else
{
instance = this;
}
DontDestroyOnLoad(this.gameObject);
}
//Play Gobal End
// Update is called once per frame
void Update () {
}
}
@sachinbirajdar Is the toggle part of this same singleton gameObject that you posted above? and how the toggle is failing? is it showing the wrong value? is it not interactable anymore?
I'm not sure if that would be a problem but the solution should look sth like this : This would be your $$anonymous$$usic Player script, that you attach to a GameObject with AudioSource Component probably at the first scene the music is supposed to start playing and the rest of the scenes where the same music should be played : public class $$anonymous$$usicPlayer : $$anonymous$$onoBehaviour {
static $$anonymous$$usicPlayer instance = null;
void Awake () {
//print("$$anonymous$$usic Plyer awake "+ GetInstanceID());
//print("$$anonymous$$usic player Start "+ GetInstanceID());
if (instance != null)
{
Destroy(gameObject);
} else
{
instance = this;
DontDestroyOnLoad(transform.root.gameObject);
}
}
// Use this for initialization
void Start(){
}
The script creates one $$anonymous$$usic Player and if there is another $$anonymous$$usic Player in the scene it destroys it, which means that you will have constant $$anonymous$$usic Playing without starting from beginning every level.
If you want to disable it, just try doing sth like this to turn this music player off:
Your Function(){
GetComponent<$$anonymous$$usicPlayer>().enabled = false;
} ;) hope it helps
Yeah....it helped me...great....thanks...
Answer by NorthStar79 · Nov 21, 2017 at 08:04 AM
you should use "audio mixer" for ultimate control of your audio.
destroying audio source object or disabling audio source component does not work because they will return their original state when scene re-loaded. but if you set audio mixers volume to "0" it will stay that way.
here a good youtube video tutorial for "audio mixer"
Your answer
Follow this Question
Related Questions
Mute Unity Ads in code? 2 Answers
AudioMixer Mute Button 1 Answer
All Unity-related audio stopped working 2 Answers
[URGENT] WebGL export completely different from editor playtesting. [C#] 1 Answer
Mute/Unmute not working with new sounds 2 Answers