Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • 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 03:54 PM · playerprefssliderawakemain menusource code

PlayerPrefs Resets Automatically upon returning to the Main Menu

My main menu has the slider. But when i switch to the next scene and return back to the main menu, my PlayerPref value does not work. I want the PlayerPrefs value to be saved when I return back from the next scene. Can someone please help me?

Here is my code on the Main Menu :

  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;
     
         public static AudioManager _instance;
      
         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, soundSlider.value);
             PlayerPrefs.SetFloat(SoundPref, soundSlider.value);
         }
     
         void OnApplicationFocus(bool inFocus)
         {
             if (!inFocus)
             {
                 SaveSoundSettings();
             }
             else
             {
                 UpdateSound();
             }
         }
     
         public void UpdateSound()
         {
             musicAudio.volume = musicSlider.value;
     
             for (int i = 0; i < soundAudio.Length; i++)
             {
                 soundAudio[i].volume = soundSlider.value;
             }
         }
     
         void Awake()
          {
              //if we don't have an [_instance] set yet
              if(!_instance)
                  _instance = this ;
              //otherwise, if we do, kill this thing
              else
                  Destroy(this.gameObject) ;
              DontDestroyOnLoad(this.gameObject) ;
          }
     
     }  

and this my code for the next 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;
 
     void Awake()
     {
         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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Jasr_88 · Jun 05, 2020 at 04:15 PM

The first time you run your game the key FirstPlay dosent exist, so will never be equal to 0, insted of:

        if (firstPlayInt == 0)

Try with:

        if (!PlayerPrefs.HasKey(FirstPlay))

Hope it helps!

Comment
Add comment · 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
0

Answer by Rebekah-D-David · Jun 05, 2020 at 05:11 PM

@Jasr_88 Thanks for your help but sadly that doesn't seem to work either. I've got an error: ========================================

NullReferenceException: Object reference not set to an instance of an object AudioManager.SaveSoundSettings () (at Assets/Scripts/AudioManager.cs:43) AudioManager.OnApplicationFocus (System.Boolean inFocus) (at Assets/Scripts/AudioManager.cs:51) UnityEditorInternal.InternalEditorUtility:OnGameViewFocus(Boolean) UnityEditor.HostView:OnLostFocus() (at C:/buildslave/unity/build/Editor/Mono/HostView.cs:170)

Comment
Add comment · Show 3 · 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 Jasr_88 · Jun 05, 2020 at 05:38 PM 1
Share

well, it's working, but you have an other error that i can only assume is: you are not setting the sound Slider property in your inspector or the reference to the property got lost. Please double check the value assigned to the soundSlider property in your inspector.

avatar image Rebekah-D-David Jasr_88 · Jun 05, 2020 at 05:52 PM 0
Share

I find the properties for my script in the inspector missing while the scene is switched during the gameplay. How can I fix this ?

avatar image Jasr_88 Rebekah-D-David · Jun 05, 2020 at 06:05 PM 0
Share

The components on the GameObjects are not persistent between scenes, if you load an other scene all the GameObjects are destroyed, and the new ones are loaded. That might cause the impresion that the values ate getting lost, i reccomend you to look up the DontDestroyOnLoad method, examples and it's usage, but that topic is way too far of the scope for this question.

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.

Follow this Question

Answers Answers and Comments

137 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 avatar image

Related Questions

Why does my slider properties go missing when switching scenes ? 1 Answer

seperate sliders for music and sound sliders 0 Answers

How to load audio slider values from PlayerPrefs at the beginning of the game? 2 Answers

Slider value saving? 1 Answer

How to save slider values? 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges