Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Gman0064 · Apr 13, 2016 at 03:17 AM · playerprefssavingsetfloat

Having issues with PlayerPrefs...

Hi all!

Im working on part of my game that allows the player to change the characters color. Ive been able to get all the variables to keep their values, but when i relaunch the game, I find that nothing has been saved at all. I have the script set so that automatically if there is no save data, the character will be white, but if that check is passed, then it will load the values saved for the respective RGB codes. If anyone can give some explanation on how this is not working and how to fix it, that would be amazing!

P.S. It is all written in C#

 using UnityEngine;
 using System.Collections;
 
 public class PlayerColorChange : MonoBehaviour {
 
     static float Red = 0.0F;
     static float Green = 0.0F;
     static float Blue = 0.0F;
 
     static int FirstPlay = 10;
 
     void Start() {
 
         if(FirstPlay == 10) {  //Checks if First Play through of game
             Debug.Log("First Playthrough, Either Fresh Install or No Save Data");
             Red = 1f;
             Green = 1f;
             Blue = 1f;
         } else {
             FirstPlay = 5;
             Debug.Log("This Works, Something is not saving correctly!");
         }
     }
 
     public void ChangeRed(float newRed) {  //Changes Red Value
         Red = newRed;
     }
 
     public void ChangeGreen(float newGreen) {  //Changes Blue Value
         Green = newGreen;
     }
 
     public void ChangeBlue(float newBlue) {  //Changes Green Value
         Blue = newBlue;
     }
 
     // Update is called once per frame
     public void Update () {
         GetComponent<SpriteRenderer>().color = new Color(Red, Green, Blue, 1f);
     }
 
     public void SaveData() {
         FirstPlay = 5;
         Debug.Log("Save Data Created");
         PlayerPrefs.SetInt("FirstPlay", FirstPlay);  //Saves the First Playthrough Value
         PlayerPrefs.SetFloat("Red", Red);  //Saves Red Value
         PlayerPrefs.SetFloat("Green", Green);  //Saves Green Value
         PlayerPrefs.SetFloat("Blue", Blue);  //Saves Blue Value
 
     }
 }
 
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
2
Best Answer

Answer by KaushikRahul · Apr 13, 2016 at 06:03 AM

Hi

You are missing

 PlayerPrefs.Save();

method.

Place it after you have Set all your PlayerPref Variable, like:

     public void SaveData() {
              FirstPlay = 5;
              Debug.Log("Save Data Created");
              PlayerPrefs.SetInt("FirstPlay", FirstPlay);  //Saves the First Playthrough Value
              PlayerPrefs.SetFloat("Red", Red);  //Saves Red Value
              PlayerPrefs.SetFloat("Green", Green);  //Saves Green Value
              PlayerPrefs.SetFloat("Blue", Blue);  //Saves Blue Value
 
              //Here
              PlayerPrefs.Save();
      
          }


And in Start, where you want to load the values from PlayerPrefs, you must use :

          void Start() {
      
              //Load FirstPlay Value Here
              FirstPlay = PlayerPrefs.GetFloat("FirstPlay", FirstPlay);
 
              if(FirstPlay == 10) {  //Checks if First Play through of game
                  Debug.Log("First Playthrough, Either Fresh Install or No Save Data");
                  Red = 1f;
                  Green = 1f;
                  Blue = 1f;
                                      
              } 

               //Load RGB Values Here
               Red = PlayerPrefs.GetFloat("Red", Red);
               Green = PlayerPrefs.GetFloat("Green", Green);
               Blue = PlayerPrefs.GetFloat("Blue", Blue);
          }


Edit: I made changes to the Start(), so the Loading of PlayerPrefs becomes more efficient, By moving the loading PlayerPrefs code out of the "If" condition.

Comment
Add comment · Show 4 · 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 Gman0064 · Apr 15, 2016 at 02:12 AM 0
Share

Thank you so much! This fixed the problem in a jiff!

avatar image Bunny83 Gman0064 · Apr 15, 2016 at 05:19 AM 1
Share

Don't forget to accept an answer if it answered your question.

avatar image KaushikRahul Gman0064 · Apr 15, 2016 at 06:00 AM 0
Share

Please accept an answer if it answered your question.

avatar image Azbo · Apr 17, 2016 at 04:48 PM 0
Share

Oh I answered below but I didn't notice he didn't call PlayerPrefs.Save();. Lol I didn't even bother checking because I though he had it!

avatar image
0

Answer by Azbo · Apr 13, 2016 at 04:11 AM

I am not sure if this will work because I just took a quick glance but maybe you should change your methods to public static void instead of public void. Because the problem could encounter when its being sent between methods. I hope this worked best of luck mate!

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

45 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

Related Questions

saving highscores 1 Answer

Saving with PlayerPrefs? How it works? 1 Answer

Saving and loading game with PlayerPrefs 1 Answer

Save Datasets at runtime 2 Answers

PlayerPrefs Name + String, is this possible? 0 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