Toggle value Changes on Scene Change
Good Day. I have created a toggle for my mute button, when i mute from the main menu then switch scenes, the sounds were all muted but the value of the toggle isn't on and the visual indicator is not kept either.
Here's my Code.
using UnityEngine;
using System.Collections;
using UnityEngine.Audio;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class SettingsMenu : MonoBehaviour {
public AudioMixer audioMixer;
public AudioSource musicSource;
[SerializeField]
private Toggle xmute;
public Slider MusicSlider, SFXSlider;
public static bool Paused = false;
public GameObject pauseUI;
void Start()
{
MusicSlider.value = PlayerPrefs.GetFloat("MusicVolume");
SFXSlider.value = PlayerPrefs.GetFloat("SFXVolume");
Paused = false;
Time.timeScale = 1f;
}
void Update()
{
PlayerPrefs.SetFloat("MusicVolume", MusicSlider.value);
PlayerPrefs.SetFloat("SFXVolume", SFXSlider.value);
}
public void Pause()
{
pauseUI.SetActive(true);
Time.timeScale = 0f;
Paused = true;
}
public void Resume()
{
pauseUI.SetActive(false);
Time.timeScale = 1f;
Paused = false;
}
public void Menu()
{
SceneManager.LoadScene("Main Menu", LoadSceneMode.Single);
Time.timeScale = 1f;
Paused = false;
}
public void Retry()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
Time.timeScale = 1f;
}
public void SetMusicVolume(float volume)
{
audioMixer.SetFloat("MusicVolume", volume);
}
public void SetSFXVolume(float volume)
{
audioMixer.SetFloat("SFXVolume", volume);
}
public void Mute(bool mute)
{
mute = !mute;
if(!mute)
{
audioMixer.SetFloat("MasterVolume", -80f);
xmute.GetComponent<UnityEngine.UI.Toggle>().isOn = true;
return;
}
else
{
audioMixer.SetFloat("MasterVolume", 0f);
xmute.GetComponent<UnityEngine.UI.Toggle>().isOn = false;
return;
}
}
}
Thanks in Advance :)
Hello @$$anonymous$$uch$$anonymous$$oreThanThis - Just pointing this out - no one has seen your implementation. You just only told us what happens, not how you've created your system...
Do you have UI in each scene?
Do you have menu that stays after scene load (Like DontDestroyOnLoad) ?
You get the idea.
Your answer
![](https://koobas.hobune.stream/wayback/20220612174109im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
UI Toggles not working on start scene 0 Answers
How do you keep a toggle highlighted/ load up a scene with the toggle in its highlighted state 1 Answer
How can I set a toggle to reverse the direction of a rotation? 0 Answers
How to enable/disable an Icon depending on the bool state of a toggle. 0 Answers