Please help! Slider values get reset to default

Hi. I’ve been trying to implement basic user settings on and off for the past week. It consists of three UI sliders (master volume, music volume, and mouse sensitivity). When I have only one slider, I’m able to load and save its value without any problems. If I have more then one, however, the other slider values after that are set to their default on load.

I originally used playerprefs to store these values but switched to json thinking the problem was with playerprefs. However, I’m still have the issue. After doing some trial and error, I discovered that this is what causes it:

volMastSlider.value = gameData.masterVolume;

Commenting this out ‘fixes’ the issue, but I need these so the slider values get set whenever the player reloads the game. Otherwise, the player has to change the volume every time they restart. Please help I’ve been stuck on this for a while. What am I doing wrong???

Here is all of the code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Audio;
using UnityEngine.UI;
using System.IO;

public class GameData{
    public float masterVolume;
    public float musicVolume;
    public float sensitivity;
}

public class optionsScript : MonoBehaviour
{
    [Header("Imported")]
    public AudioMixer masterMixer;

    [Header("Data File")]
    GameData gameData;
    string dataFilePath;

    [Header("UI Inputs")]
    [SerializeField] private Slider volMastSlider;
    [SerializeField] private Slider volMusSlider;
    [SerializeField] private Slider sensitivitySlider;

    void Start() {

        gameData = new GameData();

        dataFilePath = Application.persistentDataPath + "/GameData.json";

        LoadData();

    }


    void LoadData(){

        if(File.Exists(dataFilePath)){
            string loadGameData = File.ReadAllText(dataFilePath);
            gameData = JsonUtility.FromJson<GameData>(loadGameData);

            Debug.Log("Loading data... \n master volume: " + gameData.masterVolume + " music volume: " + gameData.musicVolume);

            //set master volume 
            masterMixer.SetFloat("masterVolume", Mathf.Log10(gameData.masterVolume) * 20);
            volMastSlider.value = gameData.masterVolume;  //ERROR: this causes the other sliders to be set to their default values for some reason

            Debug.Log("Still loading data... \n master volume: " + gameData.masterVolume + " music volume: " + gameData.musicVolume);

            //set music volume
            masterMixer.SetFloat("musicVolume", Mathf.Log10(gameData.musicVolume) * 20);
            volMusSlider.value = gameData.musicVolume;  //ERROR: this causes the other sliders to be set to their default values for some reason

            Debug.Log("Load Complete!\n master volume: " + gameData.masterVolume + " music volume: " + gameData.musicVolume);
        }
        else{
            Debug.Log("ERROR: no datafile found at " + dataFilePath + "\nSetting options to default");
        }

    }

    public void SaveData(){

        string saveGameData = JsonUtility.ToJson(gameData);
        File.WriteAllText(dataFilePath, saveGameData);

        Debug.Log("GameData was saved at " + dataFilePath + "\n master volume: " + gameData.masterVolume + " music volume: " + gameData.musicVolume);
    } 

    void DeleteData(){
        if(File.Exists(dataFilePath)){
            File.Delete(dataFilePath);

            Debug.Log("Deletion of " + dataFilePath + " was successful");
        }
        else Debug.Log("ERROR: no datafile found at " + dataFilePath);
    }

    public void SetSettings(){
        ////CHANGES SETTINGS WHEN SLIDER IS MOVED\\\\

        //master volume
        float masterVolume = volMastSlider.value;
        gameData.masterVolume = masterVolume;
        masterMixer.SetFloat("masterVolume", Mathf.Log10(volMastSlider.value) * 20);

        //music volume
        float musicVolume = volMusSlider.value;
        gameData.musicVolume = musicVolume;
        masterMixer.SetFloat("musicVolume", Mathf.Log10(volMusSlider.value) * 20);

        //sensitivity
        gameData.sensitivity = sensitivitySlider.value;

    }

}

Here are some trials that I went through to test it so you get a better idea of what happens. The code can be found in the LoadData function and both the default values are 1.

  1. Nothing commented out
            Debug.Log("Loading data... \n master volume: " + gameData.masterVolume + " music volume: " + gameData.musicVolume);

            //set master volume 
            masterMixer.SetFloat("masterVolume", Mathf.Log10(gameData.masterVolume) * 20);
            volMastSlider.value = gameData.masterVolume;  //ERROR: this causes the other sliders to be set to their default values for some reason

            Debug.Log("Still loading data... \n master volume: " + gameData.masterVolume + " music volume: " + gameData.musicVolume);

            //set music volume
            masterMixer.SetFloat("musicVolume", Mathf.Log10(gameData.musicVolume) * 20);
            volMusSlider.value = gameData.musicVolume;  //ERROR: this causes the other sliders to be set to their default values for some reason

            Debug.Log("Load Complete!\n master volume: " + gameData.masterVolume + " music volume: " + gameData.musicVolume);

trial 1

  1. volMastSlider.value is commented out
            Debug.Log("Loading data... \n master volume: " + gameData.masterVolume + " music volume: " + gameData.musicVolume);

            //set master volume 
            masterMixer.SetFloat("masterVolume", Mathf.Log10(gameData.masterVolume) * 20);
            //volMastSlider.value = gameData.masterVolume;  //ERROR: this causes the other sliders to be set to their default values for some reason

            Debug.Log("Still loading data... \n master volume: " + gameData.masterVolume + " music volume: " + gameData.musicVolume);

            //set music volume
            masterMixer.SetFloat("musicVolume", Mathf.Log10(gameData.musicVolume) * 20);
            volMusSlider.value = gameData.musicVolume;  //ERROR: this causes the other sliders to be set to their default values for some reason

            Debug.Log("Load Complete!\n master volume: " + gameData.masterVolume + " music volume: " + gameData.musicVolume);

trial 2

  1. volMusSlider.value is commented out
            Debug.Log("Loading data... \n master volume: " + gameData.masterVolume + " music volume: " + gameData.musicVolume);

            //set master volume 
            masterMixer.SetFloat("masterVolume", Mathf.Log10(gameData.masterVolume) * 20);
            volMastSlider.value = gameData.masterVolume;  //ERROR: this causes the other sliders to be set to their default values for some reason

            Debug.Log("Still loading data... \n master volume: " + gameData.masterVolume + " music volume: " + gameData.musicVolume);

            //set music volume
            masterMixer.SetFloat("musicVolume", Mathf.Log10(gameData.musicVolume) * 20);
            //volMusSlider.value = gameData.musicVolume;  //ERROR: this causes the other sliders to be set to their default values for some reason

            Debug.Log("Load Complete!\n master volume: " + gameData.masterVolume + " music volume: " + gameData.musicVolume);

trial 3

  1. Both get commented out
            Debug.Log("Loading data... \n master volume: " + gameData.masterVolume + " music volume: " + gameData.musicVolume);

            //set master volume 
            masterMixer.SetFloat("masterVolume", Mathf.Log10(gameData.masterVolume) * 20);
            //volMastSlider.value = gameData.masterVolume;  //ERROR: this causes the other sliders to be set to their default values for some reason

            Debug.Log("Still loading data... \n master volume: " + gameData.masterVolume + " music volume: " + gameData.musicVolume);

            //set music volume
            masterMixer.SetFloat("musicVolume", Mathf.Log10(gameData.musicVolume) * 20);
            //volMusSlider.value = gameData.musicVolume;  //ERROR: this causes the other sliders to be set to their default values for some reason

            Debug.Log("Load Complete!\n master volume: " + gameData.masterVolume + " music volume: " + gameData.musicVolume);

trial 4

My theory is that when calling .value, the SetSettings is triggered and changes the gameData to the initial value.

Decorate this GameData class with [System.Serializable] attribute.