Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 jcastaldo13 · Feb 10, 2015 at 05:57 PM · playerprefsslider

PlayerPrefs not saving slider data

Im trying to save my two sliders values to the playerprefs. It works until I end play mode and start again. Then its back to the default values. If I quit my game and go to the options menu while still in play mode it doesnt work then either. Is there something Im missing. Im very new to PlayerPrefs.

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class Options : MonoBehaviour {
 
     public Slider scoreSlider;
     public Slider livesSlider;
 
     public Text scoreSliderText;
     public Text livesSliderText;
 
 
     void Start()
     {
         PlayerPrefs.GetFloat("OptionScore");
         PlayerPrefs.GetFloat("OptionLives");
     }
 
     void Update()
     {
         PlayerPrefs.SetFloat("OptionScore", scoreSlider.value);
         PlayerPrefs.SetFloat("OptionLives", livesSlider.value);
 
         scoreSliderText.text = "" + PlayerPrefs.GetFloat("OptionScore");
         livesSliderText.text = "" + PlayerPrefs.GetFloat("OptionLives");
         scoreSlider.value = PlayerPrefs.GetFloat("OptionScore");
         livesSlider.value = PlayerPrefs.GetFloat("OptionLives");
 
         PlayerPrefs.Save();
 
 
 
     }
 }
Comment
Add comment · Show 1
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 smoggach · Feb 10, 2015 at 06:06 PM 0
Share

Changing PlayerPrefs from Update is a bad idea. It looks to me like your code is fine but Update runs several times per frame. You're probably saving values from some point during it's destruction.

Logically you only need to do this once each time you get rid of this particular component.

3 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by DiegoSLTS · Feb 10, 2015 at 06:06 PM

You're saving the slider.value on every Update, when you run the game the slider is at the default position and the first Update saves that position, overwriting the one you saved before.

I see you're getting those values on the Start method, I guess it's to initialize the sliders to the values stored in PlayerPrefs, but you're just getting the values, you're not doing anything with them.

Try with this Start method:

 void Start()
 {
     scoreSlider.value = PlayerPrefs.GetFloat("OptionScore");
     livesSlider.value = PlayerPrefs.GetFloat("OptionLives");
 }

Also, storing the values on EVERY Update looks like a bad idea (a waste of resources and may be the source of bugs), look at the onValueChanged property of the slider, you can even set it up in the Inspector.

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 jcastaldo13 · Feb 10, 2015 at 06:16 PM 0
Share

Thank you, that fixed it. How do you use the onValueChanged property?

avatar image DiegoSLTS · Feb 10, 2015 at 06:36 PM 0
Share

Select a slider on the hierarchy and look at the "Slider" component on inspector. At the bottom it has a section that says "On Value Changed" with a + and - at the bottom right. Press the + button and it'll let you choose a game object, then any script on that game object, and then any function that can be used.

You can add multiple functions to this and they'll be called one after another.

avatar image jcastaldo13 · Feb 10, 2015 at 06:43 PM 0
Share

Wow, cool. I will definitely try that out. Thank you

avatar image
0

Answer by giulio-pierucci · Feb 10, 2015 at 06:24 PM

You should't use PlayerPrefs.Save in Update! By the Way, i think the problem is in Start():

 void Start()
      {
          scoreSlider.value = PlayerPrefs.GetFloat("OptionScore");
          livesSlider.value = PlayerPrefs.GetFloat("OptionLives");
      }
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 TooManySugar · May 10, 2017 at 01:37 PM

Something I've noticed (unity 4.7.2) Is that by the time you whant to save slider values to player prefs you've to do it individually rather than in bulk, this way

     public void ChaseCamDist_Save(){
         distance = DistanceSlicer.value;
         Debug.Log ("ChaseCamDist" + PlayerPrefs.GetFloat("ChaseCamDist"));
         PlayerPrefs.SetFloat ("ChaseCamDist", distance);
     }
 
     public void ChaseCamHeight_Save(){
         height = HeightSlider.value;
         Debug.Log ("ChaseCamHeight" + PlayerPrefs.GetFloat("ChaseCamHeight"));
         PlayerPrefs.SetFloat ("ChaseCamHeight",height);
     }

I was saving this two sliders with a single func and allways the second one failed and got the devault value instead. This happened me in teh past too.

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

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

22 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

Related Questions

How to save slider values? 3 Answers

Problems with pulling a slider value - (Making sensitivity Sliders) 0 Answers

PlayerPrefs Resets Automatically upon returning to the Main Menu 2 Answers

Getting a slider value from a different scene 1 Answer

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


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