Audio Mixer Volume on Slider isn't saved after changing Scenes?

I’ll put in here my whole code(it’s Videosettings and Audiosettings, I know I’m lazy)
And maybe one of you can tell me what I have to code, that my Audio Volume gets saved after changing scenes.
So when i go into the game, the slider returns it’s Position to the top and the Volume is on the highest Volume.
Maybe you could then upload the updated script in here son i can analyze it.
Im thankful for every help.
The code for the Volume starts at the top with the public audiomixer and the setvolume code etc is all the way at the bottom.
Sincerely Marlon

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

public class AllOptions : MonoBehaviour{

    public AudioMixer audioMixer;

    public Dropdown ResolutionDropdown;

    Resolution[] resolutions;

    void Start ()
    {
       resolutions = Screen.resolutions;

       ResolutionDropdown.ClearOptions();

       List<string> options = new List<string>();

        int currentResolutionIndex = 0;
   
       
       for (int i = 0; i < resolutions.Length; i++)
       {
           string option = resolutions[i].width + " x " + resolutions[i].height;
           options.Add(option); 
          
            if (resolutions[i].width == Screen.currentResolution.width &&
            resolutions[i].height == Screen.currentResolution.height)
            {
                currentResolutionIndex = i;
            }

       }

        ResolutionDropdown.AddOptions(options);
        ResolutionDropdown.value = currentResolutionIndex;
        ResolutionDropdown.RefreshShownValue();
   
    }

    public void SetResolution (int resolutionIndex)
    {
        Resolution resolution = resolutions[resolutionIndex];
        Screen.SetResolution(resolution.width, resolution.height, Screen.fullScreen);
    }

    public void SetVolume(float volume)
   
   
    {
        audioMixer.SetFloat("MenuVolume",volume);
    }

    public void SetQuality (int qualityIndex)
    {
        QualitySettings.SetQualityLevel(qualityIndex);
    }

    public void SetFullscreen (bool isFullscreen)
    {
        Screen.fullScreen = isFullscreen;
    }
       
   
}

I would save out the volume to a playerpref and then load that playerpref at the start to get the previous set volume. Just look up Unity PlayerPrefs to learn more about it.

I agree with Brath … this is the easiest way to go.

Here’s an example of simple persistent loading/saving values using PlayerPrefs:

https://pastebin.com/icJSq5zC

Useful for a relatively small number of simple values.

1 Like