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 /
  • Help Room /
avatar image
0
Question by Bleackk · Apr 13, 2016 at 05:07 PM · c#playerprefssaveload

PlayerPrefs saving and Loading

I got this little code. I made it so it'll save the Floats and It's from different values. Now, what am I missing? I'm very Newb'ish when it comes to PlayerPrefs.

 using UnityEngine;
 using System.Collections;
 
 public class SaveState : MonoBehaviour {
 
     public AgeCounter ageC;
     public Click click;
     public EXP exp;
     public overallPoops poops;
 
 
     public void Save()
     {
         PlayerPrefs.SetInt ("Age", ageC.ageNum );
         PlayerPrefs.SetInt ("Age2", ageC.targetExp);
         PlayerPrefs.SetInt ("GPC", click.goldperclick);
         PlayerPrefs.SetInt ("exp", exp.experience);
         PlayerPrefs.SetInt ("expPS", exp.experiencePS);
 
         PlayerPrefs.SetFloat ("Gold", click.gold);
         PlayerPrefs.SetFloat ("OverallG", click.overalGold );
         PlayerPrefs.SetFloat ("GPC", click.goldperclick);
         PlayerPrefs.SetFloat ("Health", poops.health);
     }
     public void Load()
     {
         int Age = PlayerPrefs.GetInt ("Age", ageC.ageNum);
         int Age2 = PlayerPrefs.GetInt ("Age2", ageC.targetExp);
         int GPC = PlayerPrefs.GetInt ("GPC", click.goldperclick);
         int exp = PlayerPrefs.GetInt ("exp");
         int expPS = PlayerPrefs.GetInt ("expPS");
 
         float gold = PlayerPrefs.GetFloat ("Gold");
         float overallG = PlayerPrefs.GetFloat ("OverallG");
         float gpc = PlayerPrefs.GetFloat ("GPC");
         float health = PlayerPrefs.GetFloat ("Health");
     }
 
 }
 


what did i Miss in Loading?

Comment
Add comment · Show 2
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 jgodfrey · Apr 13, 2016 at 05:30 PM 0
Share

Please, don't make us guess about what the problem is - just tell us. Do you get a compiler error, a runtime error, no error but not proper result, ????

avatar image Bleackk jgodfrey · Apr 15, 2016 at 09:58 PM 0
Share

It's straight forward what's the problem though. The Load Section is wrong.

2 Replies

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

Answer by Jessespike · Apr 13, 2016 at 06:03 PM

The Load function is storing data in local variables, once the Load function completes, all the data you retrieved is lost.

Change int Age = PlayerPrefs.GetInt ("Age", ageC.ageNum);

to ageC.ageNum = PlayerPrefs.GetInt ("Age", 0);

The "0" is the default value to use, if Age doesn't exist in PlayerPrefs.


  • Unity Documentation - PlayerPrefs

  • Guide to PlayerPrefs

  • Unity Answers "playerprefs"

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 Bleackk · Apr 14, 2016 at 11:44 AM 0
Share

So, when tehre is NO data then Get(Int,String,Float) will get the default value that i set after the saved PlayerPrefs. Right? For example like you said, default start age is 0, no data yet saved so it'll use the Default value 0 that i set in the Script. If so, your answer gets the top since i allready figured out the loading process.

avatar image Jessespike Bleackk · Apr 14, 2016 at 06:03 PM 0
Share

That is correct. If the PlayerPref cannot be found, then the default value is used ins$$anonymous$$d.

avatar image Bleackk Jessespike · Apr 15, 2016 at 04:47 PM 0
Share

Well. To the Top with you. Here's how I figured everything out with all you guys's help.

Show more comments
avatar image
0

Answer by M-Hanssen · Apr 13, 2016 at 06:57 PM

This is correct:

  using UnityEngine;
  using System.Collections;
 
 public class SaveState : MonoBehaviour
 {
 
     public AgeCounter ageC;
     public Click click;
     public EXP exp;
     public overallPoops poops;
 
     public void Save()
     {
         PlayerPrefs.SetInt("Age", ageC.ageNum);
         PlayerPrefs.SetInt("Age2", ageC.targetExp);
         PlayerPrefs.SetInt("GPC", click.goldperclick);
         PlayerPrefs.SetInt("exp", exp.experience);
         PlayerPrefs.SetInt("expPS", exp.experiencePS);
 
         PlayerPrefs.SetFloat("Gold", click.gold);
         PlayerPrefs.SetFloat("OverallG", click.overalGold);
         PlayerPrefs.SetFloat("GPC", click.goldperclick);
         PlayerPrefs.SetFloat("Health", poops.health);
     }
 
     public void Load()
     {
         int Age = PlayerPrefs.GetInt("Age");
         int Age2 = PlayerPrefs.GetInt("Age2");
         int GPC = PlayerPrefs.GetInt("GPC");
         int exp = PlayerPrefs.GetInt("exp");
         int expPS = PlayerPrefs.GetInt("expPS");
 
         float gold = PlayerPrefs.GetFloat("Gold");
         float overallG = PlayerPrefs.GetFloat("OverallG");
         float gpc = PlayerPrefs.GetFloat("GPC");
         float health = PlayerPrefs.GetFloat("Health");
     }
 
 }

And indeed only use the second parameter if you want to set a default value.

Comment
Add comment · Show 2 · 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 Bleackk · Apr 14, 2016 at 11:45 AM 0
Share

Thanks. however you still need to specify where does the Int and Float go,

something like this

 exp.experience = exp;



avatar image M-Hanssen Bleackk · Apr 14, 2016 at 12:52 PM 0
Share

Are you seriously asking because you don't know? Here it is:

 public class SaveState : $$anonymous$$onoBehaviour
 {
     public AgeCounter ageC;
     public Click click;
     public EXP exp;
     public overallPoops poops;
 
     public void Save()
     {
         PlayerPrefs.SetInt("Age", ageC.ageNum);
         PlayerPrefs.SetInt("Age2", ageC.targetExp);
         PlayerPrefs.SetInt("GPC", click.goldperclick);
         PlayerPrefs.SetInt("exp", exp.experience);
         PlayerPrefs.SetInt("expPS", exp.experiencePS);
 
         PlayerPrefs.SetFloat("Gold", click.gold);
         PlayerPrefs.SetFloat("OverallG", click.overalGold);
         PlayerPrefs.SetFloat("GPC", click.goldperclick);
         PlayerPrefs.SetFloat("Health", poops.health);
     }
 
     public void Load()
     {
         ageC.ageNum = PlayerPrefs.GetInt("Age");
         ageC.targetExp = PlayerPrefs.GetInt("Age2");
         click.goldperclick = PlayerPrefs.GetInt("GPC");
         exp.experience = PlayerPrefs.GetInt("exp");
         exp.experiencePS = PlayerPrefs.GetInt("expPS");
 
         click.gold = PlayerPrefs.GetFloat("Gold");
         click.overalGold = PlayerPrefs.GetFloat("OverallG");
         click.goldperclick = PlayerPrefs.GetFloat("GPC");
         poops.health = PlayerPrefs.GetFloat("Health");
     }
 }

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

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

Related Questions

Continue from where you left with PlayerPRefs 2 Answers

save a int with playerprefs for one instance of a gameobject 0 Answers

Help offline progression, works on windows but not on Android. 0 Answers

Playerpref Saving Lag 1 Answer

Save game - Tile based game 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