• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Rebekah-D-David · Jun 05, 2020 at 08:15 PM · playerprefssliderdontdestroyonloadmain menuswitch scenes

Why does my slider properties go missing when switching scenes ?

My slider properties are missing when i switch scene from the main menu. Why does that happen ? Here's my code on the main menu where my Slider is:

  using UnityEngine;
     using UnityEngine.UI;
     
     public class AudioManager : MonoBehaviour
     {
         private static readonly string FirstPlay = "FirstPlay";
         private static readonly string MusicPref = "MusicPref";
         private static readonly string SoundPref = "SoundPref";
         private int firstPlayInt;
         public Slider musicSlider, soundSlider;
         private float musicFloat, soundFloat;
         public AudioSource musicAudio;
         public AudioSource[] soundAudio;
     
         private static AudioManager instance = null;
     
         void Start()
         {
     
             firstPlayInt = PlayerPrefs.GetInt(FirstPlay);
     
             if (firstPlayInt == 0)
             {
                 musicFloat = .125f;
                 soundFloat = .75f;
                 musicSlider.value = musicFloat;
                 soundSlider.value = soundFloat;
                 PlayerPrefs.SetFloat(MusicPref, musicFloat);
                 PlayerPrefs.SetFloat(SoundPref, soundFloat);
                 PlayerPrefs.SetInt(FirstPlay, -1);
             }
             else
             {
                 musicFloat = PlayerPrefs.GetFloat(MusicPref);
                 musicSlider.value = musicFloat;
                 soundFloat = PlayerPrefs.GetFloat(SoundPref);
                 soundSlider.value = soundFloat;
             }
     
         }
     
         public void SaveSoundSettings()
         {
             PlayerPrefs.SetFloat(MusicPref, musicSlider.value);
             PlayerPrefs.SetFloat(SoundPref, soundSlider.value);
         }
     
         void OnApplicationFocus(bool inFocus)
         {
             if (!inFocus)
             {
                 SaveSoundSettings();
             }
         }
     
         public void UpdateSound()
         {
             musicAudio.volume = musicSlider.value;
     
             for (int i = 0; i < soundAudio.Length; i++)
             {
                 soundAudio[i].volume = soundSlider.value;
             }
         }
     
         private void Awake()
         {
             if (instance == null)
             {
                 instance = this;
                 DontDestroyOnLoad(gameObject);
                 return;
             }
             if (instance == this) return;
             {
                 Destroy(gameObject);
             }
         }
     }  


and here's my code on the other scene :

 using UnityEngine;
 
 public class AudioSetting : MonoBehaviour
 {
 
     private static readonly string MusicPref = "MusicPref";
     private static readonly string SoundPref = "SoundPref";
     private float musicFloat, soundFloat;
     public AudioSource musicAudio;
     public AudioSource[] soundAudio;
 
     private void Start()
     {
         Destroy(FindObjectOfType<AudioSetting>());
     }
 
     private void Awake()
     {
         ContinueSettings();
     }
 
 
  private void ContinueSettings()
     {
         musicFloat = PlayerPrefs.GetFloat(MusicPref);
         soundFloat = PlayerPrefs.GetFloat(SoundPref);
 
         musicAudio.volume = musicFloat;
 
         for(int i = 0; i < soundAudio.Length; i++)
         {
             soundAudio[i].volume = soundFloat;
         }
     }
 
 }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Bunny83 · Jun 06, 2020 at 09:51 AM

Well since this is a pseudo singleton class you are carrying it with you to the next scene. Are your sliders child objects of that singleton or at least on another singleton that is marked with DontDestroyOnLoad? If not of course your slider gameobject will be destroyed when you change scenes.


Apart from that note that you should call PlayerPrefs.Save() after you made some changes in order to actually flush those changes to disk. Otherwise if your application is terminated those changes might not be saved.


Finally instead of using that "FirstPlayInt" you could just use a default value in your get methods when you read then in the first place. That way your start method would just turn into:

 musicFloat = PlayerPrefs.GetFloat(MusicPref, 0.125f);
 soundFloat = PlayerPrefs.GetFloat(SoundPref, 0.75f);
 
 musicSlider.value = musicFloat;
 soundSlider.value = soundFloat;

Though I usually use this approach: You initialize your variables with a default value in the field initializer and just use GetFloat like this:

 musicFloat = PlayerPrefs.GetFloat(MusicPref, musicFloat);

So you only overwrite the value if it has been saved, otherwise it will keep it's initial value. That way the user could delete individual settings (on windows with regedit) and on the next run only that one value would get its default value while the others stay.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Rebekah-D-David · Jun 06, 2020 at 12:04 PM 0
Share

@Bunny83 How do I mark my child objects of the singleton with DontDestroyOnLoad. Can you help me with that? I tried many different methods. I just don;t understand what is not working..

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

136 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

PlayerPrefs Resets Automatically upon returning to the Main Menu 2 Answers

PlayerPrefs not saving slider data 3 Answers

DontDestroyOnLoad reset the gameobject 1 Answer

What is the standard solution for preserving level data? 0 Answers

Slider value not working? 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges