- Home /
C# Mute Button across scenes Problem
Hi, I have been trying to solve this problem for a few days now...No joy.
I have followed this tutorial to get Background that changes across my levels using an AudioManager [MEDIA=youtube]uNX5NrHvfWY|start=451[/MEDIA]
AudioManager
[code=CSharp]using System.Collections; using System.Collections.Generic; using UnityEngine;
public class AudioManager : MonoBehaviour {
public AudioSource BackGroundMusic;
// Use this for initialization
void Start () {
DontDestroyOnLoad (gameObject);
if (FindObjectsOfType<AudioManager> ().Length > 1)
{
Destroy (gameObject);
}
}
// Update is called once per frame
void Update () {
}
public void ChangeBGM (AudioClip music)
{
if (BackGroundMusic.clip.name == music.name)
return;
BackGroundMusic.Stop ();
BackGroundMusic.clip = music;
BackGroundMusic.Play ();
BackGroundMusic.loop = true;
}
} [/code]
AudioManagerCheck
[code=CSharp]using System.Collections; using System.Collections.Generic; using UnityEngine;
public class AudioManagerCheck : MonoBehaviour {
public GameObject audioMan;
// Use this for initialization
void Start () {
if (FindObjectOfType<AudioManager> ())
return;
else
Instantiate (audioMan, transform.position, transform.rotation);
}
// Update is called once per frame
void Update () {
}
} [/code]
SwitchMusicOnLoad
[code=CSharp]using System.Collections; using System.Collections.Generic; using UnityEngine;
public class SwitchMusicOnLoad : MonoBehaviour {
public AudioClip newTrack;
private AudioManager TheAudioManager;
// Use this for initialization
void Start () {
TheAudioManager = FindObjectOfType<AudioManager> ();
if(newTrack != null)
TheAudioManager.ChangeBGM (newTrack);
}
// Update is called once per frame
void Update () {
}
} [/code]
And now I have followed this tutorial [MEDIA=youtube]ABqmWhD5Ny8[/MEDIA]
MuteButton
[code=CSharp]using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEditor;
public class MuteButton : MonoBehaviour {
public GameObject audioOnIcon;
public GameObject audioOffIcon;
// Use this for initialization
void Start () {
SetSoundState ();
}
// Update is called once per frame
void Update () {
}
public void ToggleSound()
{
if (PlayerPrefs.GetInt ("Muted", 0) == 0) {
PlayerPrefs.SetInt ("Muted", 1);
}
else
{
PlayerPrefs.SetInt ("Muted", 0);
}
SetSoundState ();
}
private void SetSoundState()
{
if (PlayerPrefs.GetInt ("Muted", 0) == 0)
{
AudioListener.volume = 1;
audioOnIcon.SetActive (true);
audioOffIcon.SetActive (false);
}
else
{
AudioListener.volume = 0;
audioOnIcon.SetActive (false);
audioOffIcon.SetActive (true);
}
}
} [/code]
so I can save the toggle of a mute button.
The problem is, I need to access the AudioSource on the AudioManager/AudioManager clone via the Mute Button and have it mute the AudioSource and not the AudioListener, because the background music is controlled by the AudioManager and I have other sounds that I would like to keep playing when mute is toggled on).
I hope I have given enough detail for one of you amazing C# experts to help me solve. Thank you,
Sam
This is a test the Go_1 sound is the sfx sound that should still be playing after mute is toggled on or off.
Your answer
![](https://koobas.hobune.stream/wayback/20220612142717im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
PlayerPrefs for Mute/Unmute button 1 Answer
Sound volume on different colisions 1 Answer
C# to find a Object in a direction on a 2D map 0 Answers
convert gradient in to color keys 1 Answer