I have a UI MENU where the player can set the volume of the game music.
Through a slider it can change the volume. I'm trying to save these values, and retrieves them whenthe game is open again, but without success so far.
When we change the value of the Slider, the sound also changes, but these values are not being saved.
My code looks like this:
Ps: 0 warning in Unity console, and C# answer if possible =]
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class PauseCanvas : MonoBehaviour
{
public Button resumeButton;
public Button backToMainMenuButton;
public Button exitGameButton;
public Canvas gameCanvas;
public Canvas mainMenuCanvas;
public Slider slider;
public float startVolume = 1f;
public float currentVolume;
string newVolume;
void Awake () {
slider.value = GetComponent<AudioSource> ().volume;
currentVolume = slider.value;
}
void Start () {
startVolume = PlayerPrefs.GetFloat (newVolume, 1);
}
void UpdateVolume() {
if (currentVolume < startVolume ) {
PlayerPrefs.SetFloat (newVolume, currentVolume);
PlayerPrefs.Save ();
}
}
void OnEnable ()
{
//Register Button Events
resumeButton.onClick.AddListener (() => buttonCallBack (resumeButton));
backToMainMenuButton.onClick.AddListener (() => buttonCallBack (backToMainMenuButton));
exitGameButton.onClick.AddListener (() => buttonCallBack (exitGameButton));
}
private void buttonCallBack (Button buttonPressed)
{
//Resume Button Pressed
if (buttonPressed == resumeButton) {
Time.timeScale = 1;
//Hide this Pause Canvas
gameObject.SetActive (false);
//Show Game Canvas
gameCanvas.gameObject.SetActive (true);
}
//Back To Main Menu Button Pressed
if (buttonPressed == backToMainMenuButton) {
//Hide this Pause Canvas
gameObject.SetActive (false);
//Show Main Menu Canvas
Score.Inicializar ();
SceneManager.LoadScene ("Menu");
}
//Exit Game Button Pressed
if (buttonPressed == exitGameButton) {
#if UNITY_EDITOR
UnityEditor.EditorApplication.isPlaying = false;
#else
Application.Quit();
#endif
}
}
void OnDisable ()
{
//Un-Register Button Events
resumeButton.onClick.RemoveAllListeners ();
backToMainMenuButton.onClick.RemoveAllListeners ();
exitGameButton.onClick.RemoveAllListeners ();
}
}