Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

437
Views
In Unity, I have made a working slider for volume that keeps it's value upon scene change. When the game is built, it no longer saves the value

I'm really new to using Unity and C#, I'll do my best to explain the situation and show relevant code.

So basically I am creating a Settings Menu in my game. Options I have are a fullscreen toggle, resolution dropdown, graphics dropdown and volume slider. All of these settings will maintain the selection on a scene change or the game being shutdown and run again, other than the volume slider.

In Unity, the volume slider will maintain it's selection. Just not in the game once it has been built. It will instead default back to middle of the slider.

Here is relevant code.

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

    public class SettingsMenu : MonoBehaviour
    {
        public AudioMixer audioMixer;
        public Slider volumeSlider;

        private void Start()
        {
             volumeSlider.value = GetVolume();
        }

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

public float GetVolume()
{
    bool result = audioMixer.GetFloat("volume", out float value);
    if (result == true)
    {
        return value;
    }
    else
    {
        return -40f;
    }
}

Now as I have said, this all works fine within Unity itself, the issue arises when I build the game, changing scenes means that the slider just defaults.

Please let me know if you need any more information. Thanks in advance.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You are close, use PlayerPrefs to store the last value that was set.

Something like this:

public class MyClass
{
    private const string VolumePreferenceKey = "preferred_volume";

    private const string VolumeKey = "volume";

    public AudioMixer Mixer;

    public float GetVolume()
    {
        if (Mixer == null)
        {
            Debug.LogWarning("Mixer property is not set !");

            return -1.0f;
        }

        if (Mixer.GetFloat(VolumeKey, out var volume))
        {
            return volume;
        }

        Debug.LogWarning("Mixer volume couldn't be retrieved !");

        return -1.0f;
    }

    public void SetVolume(float volume)
    {
        if (Mixer == null)
        {
            Debug.LogWarning("Mixer property is not set!");
            return;
        }

        if (!Mixer.SetFloat(VolumeKey, volume))
        {
            Debug.LogWarning("Mixer volume couldn't be set !");
            return;
        }

        PlayerPrefs.SetFloat(VolumePreferenceKey, volume);
    }
}

I wrote this quickly, adjust it to your needs.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!