Hello! I don’t know how to reset my settings on my game when quitting with a QUIT button. What I mean is that I want to press QUIT button and when the game is out the settings have to be default, when I will reenter the game again. For example, on my game I have made changes in settings, lets say volume slider and when I will exit my game and reenter it then I will have my settings saved and not default. I want it to be like the second screenshot when I quit and reentering the game.
I need help with that because I can’t continue my progress and I stuck with that!!
Check all my code here and screenshots for more information:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using TMPro;
using UnityEngine.Audio;
using System.ComponentModel.Design;
using Unity.VisualScripting;
using System;
public class SettingsManager : MonoBehaviour
{
// Variables that we are using in our code!
[SerializeField] private Slider menuMusicVolumeSlider;
[SerializeField] private TextMeshProUGUI menuMusicVolumeSliderText;
[SerializeField] private Slider soundsVolumeSlider;
[SerializeField] private TextMeshProUGUI soundsVolumeSliderText;
[SerializeField] private Slider gameVolumeSlider;
[SerializeField] private TextMeshProUGUI gameVolumeSliderText;
[SerializeField] private Slider masterVolumeSlider;
[SerializeField] private TextMeshProUGUI masterVolumeSliderText;
public AudioMixer myAudioMixer;
const string menuMusicVol = "MenuMusicVolume";
const string soundEffectsVol = "SoundsVolume";
const string masterVol = "MasterVolume";
const string gameMusicVol = "GameMusicVolume";
// Start is called before the first frame update
void Start()
{
//Every variable or initialization that has to happen when the game starts in here!
LoadSettings();
}
public void SaveSettings()
{
//Here we save all of our settings!
float menuMusicVolumeValue = menuMusicVolumeSlider.value;
float soundsVolumeValue = soundsVolumeSlider.value;
float masterVolumeValue = masterVolumeSlider.value;
PlayerPrefs.SetFloat("MenuMusicVolume", menuMusicVolumeValue);
PlayerPrefs.SetFloat("SoundsVolume", soundsVolumeValue);
PlayerPrefs.SetFloat("MasterVolume", masterVolumeValue);
myAudioMixer.SetFloat(soundEffectsVol, Mathf.Log10(soundsVolumeValue) * 20);
myAudioMixer.SetFloat(menuMusicVol, Mathf.Log10(menuMusicVolumeValue) * 20);
myAudioMixer.SetFloat(masterVol, Mathf.Log10(masterVolumeValue) * 20);
Debug.Log("Settings are saved and audio mixer is working!");
}
public void LoadSettings()
{
//Here we load all of our saved settings!
float menuMusicVolumeValue = PlayerPrefs.GetFloat("MenuMusicVolume");
float soundsVolumeValue = PlayerPrefs.GetFloat("SoundsVolume");
float masterVolumeValue = PlayerPrefs.GetFloat("MasterVolume");
if (menuMusicVolumeSlider != null)
{
menuMusicVolumeSlider.value = menuMusicVolumeValue;
}
if (soundsVolumeSlider != null)
{
soundsVolumeSlider.value = soundsVolumeValue;
}
if (masterVolumeSlider != null)
{
masterVolumeSlider.value = masterVolumeValue;
}
Debug.Log("Saved settings has been loaded!");
}
public void MenuMusicVolumeSlider()
{
float menuVolume = menuMusicVolumeSlider.value;
menuMusicVolumeSliderText.text = menuVolume.ToString("0.0");
Debug.Log("Display of main menu volume text as string!");
}
public void GameVolumeSlider()
{
float gameVolume = gameVolumeSlider.value;
gameVolumeSliderText.text = gameVolume.ToString("0.0");
myAudioMixer.SetFloat(gameMusicVol, Mathf.Log10(gameVolumeSlider.value) * 20);
Debug.Log("Display of game volume text as string!");
}
public void SoundsVolumeSlider()
{
float soundsVolume = soundsVolumeSlider.value;
soundsVolumeSliderText.text = soundsVolume.ToString("0.0");
Debug.Log("Display of sounds volume text as string!");
}
public void MasterVolumeSlider()
{
float masterVolume = masterVolumeSlider.value;
masterVolumeSliderText.text = masterVolume.ToString("0.0");
Debug.Log("Display of master volume text as string!");
}
public void ResetSettings()
{
//Here we are resetting all of our settings back to it's default value!
if (menuMusicVolumeSlider != null)
{
menuMusicVolumeSlider.value = 1.0f;
myAudioMixer.SetFloat(menuMusicVol, Mathf.Log10(menuMusicVolumeSlider.value) * 20);
}
if (soundsVolumeSlider != null)
{
soundsVolumeSlider.value = 1.0f;
myAudioMixer.SetFloat(soundEffectsVol, Mathf.Log10(soundsVolumeSlider.value) * 20);
}
if (masterVolumeSlider != null)
{
masterVolumeSlider.value = 1.0f;
myAudioMixer.SetFloat(masterVol, Mathf.Log10(masterVolumeSlider.value) * 20);
}
Debug.Log("Reset settings to default!");
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEditor;
using UnityEngine.SceneManagement;
using TMPro;
using Unity.VisualScripting;
using UnityEngine.PlayerLoop;
using UnityEngine.Rendering;
using UnityEngine.Audio;
public class UIManager : MonoBehaviour
{
// Initialization of some variables here!
public bool gameIsActive;
public bool pauseIsActive;
public bool gameIsOver;
private float pauseMenuScreenDelay = 0.1f;
private float continueButtonDelay = 0.1f;
private float quitButtonDelay = 0.1f;
private float optionsButtonDelay = 0.1f;
private float playButtonDelay = 0.1f;
private float backButtonDelay = 0.1f;
private float homeBlackButtonDelay = 0.1f;
private float restartButtonDelay = 0.2f;
public GameObject pauseMenuScreen;
public SettingsManager settingsManager;
public AudioManager audioManager;
public Button quitButton;
public Button exitButton;
public Button playButton;
public Button optionsButton;
public Button pauseButton;
public Button continueButton;
public Button homeMenuButton;
public Button applyButton;
public Button resetButton;
public Button loadButton;
// Start is called before the first frame update
public void StartGame()
{
settingsManager = GameObject.Find("SettingsManager").GetComponent<SettingsManager>();
audioManager = GameObject.Find("AudioManager").GetComponent<AudioManager>();
gameIsActive = true;
pauseMenuScreen.SetActive(false);
Debug.Log("The game started!");
}
void Update()
{
InputForPauseMenuScreen();
}
public void QuitGame()
{
StartCoroutine(QuitAfterDelay());
}
IEnumerator QuitAfterDelay()
{
yield return new WaitForSecondsRealtime(quitButtonDelay);
Application.Quit();
EditorApplication.ExitPlaymode();
Debug.Log("Game quits and editor play mode stops!");
}
public void LoadOptionsScene()
{
StartCoroutine(LoadOptionsSceneAfterDelay());
Debug.Log("Options scene is being loaded!");
}
IEnumerator LoadOptionsSceneAfterDelay()
{
yield return new WaitForSecondsRealtime(optionsButtonDelay);
SceneManager.LoadScene("OptionsScene");
Debug.Log("Options Button is pressed after a delay!");
}
public void LoadMainGameScene()
{
StartCoroutine(LoadMainGameAfterDelay());
Destroy(GameObject.FindWithTag("MainMenuBackgroundMusic"));
}
IEnumerator LoadMainGameAfterDelay()
{
yield return new WaitForSecondsRealtime(playButtonDelay);
SceneManager.LoadScene("MainScene");
Time.timeScale = 1f;
Debug.Log("Play Button is pressed after a delay!");
}
public void GoBackButton()
{
StartCoroutine(GoBackToMainMenuSceneAfterDelay());
}
IEnumerator GoBackToMainMenuSceneAfterDelay()
{
yield return new WaitForSecondsRealtime(backButtonDelay);
SceneManager.LoadScene("MainMenuScene");
Debug.Log("Left back button is pressed after a delay and back button is pressed!");
}
public void ContinueGameButton()
{
StartCoroutine(ContinueGameAfterDelay());
}
IEnumerator ContinueGameAfterDelay()
{
yield return new WaitForSecondsRealtime(continueButtonDelay);
pauseMenuScreen.SetActive(false);
Time.timeScale = 1f;
Debug.Log("Continue button is pressed and pause menu screen gone!");
}
public void InputForPauseMenuScreen()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
if (pauseIsActive)
{
StartCoroutine(ResumeAfterDelay());
}
else
{
StartCoroutine(PauseAfterDelay());
}
}
}
IEnumerator ResumeAfterDelay()
{
yield return new WaitForSecondsRealtime(pauseMenuScreenDelay);
pauseIsActive = false;
pauseMenuScreen.SetActive(false);
Time.timeScale = 1f;
audioManager.mainGameMusicAudioSource.Play();
Debug.Log("Pause menu screen is disabled after delay!");
}
IEnumerator PauseAfterDelay()
{
yield return new WaitForSecondsRealtime(pauseMenuScreenDelay);
pauseIsActive = true;
pauseMenuScreen.SetActive(true);
Time.timeScale = 0f;
audioManager.mainGameMusicAudioSource.Stop();
Debug.Log("Pause menu screen is enabled after delay!");
}
public void PauseBlackButton()
{
if (!pauseIsActive)
{
pauseIsActive = true;
pauseMenuScreen.SetActive(true);
Time.timeScale = 0f;
audioManager.mainGameMusicAudioSource.Stop();
Debug.Log("Pause menu screen is enabled with left click!");
}
else if (pauseIsActive)
{
pauseIsActive = false;
pauseMenuScreen.SetActive(false);
Time.timeScale = 1f;
audioManager.mainGameMusicAudioSource.Play();
Debug.Log("Pause menu screen is disabled with left click!");
}
}
public void HomeBlackButton()
{
StartCoroutine(HomeBlackButtonAfterDelay());
}
IEnumerator HomeBlackButtonAfterDelay()
{
yield return new WaitForSecondsRealtime(homeBlackButtonDelay);
SceneManager.LoadScene("MainMenuScene");
Debug.Log("Main menu scene is loaded and home black button is pressed!");
}
public void RestartButton()
{
StartCoroutine(RestartButtonAfterDelay());
}
IEnumerator RestartButtonAfterDelay()
{
yield return new WaitForSecondsRealtime(restartButtonDelay);
SceneManager.LoadScene("MainScene");
Debug.Log("Restart button pressed and loaded main game scene and game over menu scree gone!");
}
}