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 Igor_Vasiak · Apr 25, 2017 at 04:36 PM · playerprefsloadsave databugsstats

PlayerPrefs saving, but not loading some values

I do use PlayerPrefs for everything (even knowing that it should be better to use XML), but it just doesn't works correctly! I mean, I have these attributes:

 private int stamna;
 private int health;
 private float speed;
 private float speed; //I have one script to move on X and one to move on Z axes.
 private float defence;
 private float dmg;

And also have the variables that display the points applyed to each skill during game, each one of them I use at SIX objects, and save individually, since I have a Helmet, a Chestplate, Gloves, Weapon, Pants, Boots:

 private int manaPoints;
 private int healthPoints;
 private int speedPoints;
 private int defencePoints;
 private int damagePoints;
 private int pointsToSpend;
 private int currentXP;
 private int xpToNxtLvl;

I save them like this:

Equipment:

 PlayerPrefs.SetInt(save_load.HeroName + gameObject.name + "SkillKey", theSkill);

Example:

 PlayerPrefs.SetInt("*GalahadHelmetDamageKey", damagePoints);

Player stats:

 PlayerPrefs.SetInt(save_load.HeroName + "StatKey", theStat);

Example:

 PlayerPrefs.SetInt("*GalahadDamageKey", dmg);

And I load them with PlayerPrefs.GetInt (in the case of speed I use GetFloat). Some of the values load and some doesn't:

 //Stamna doesn't load correctly, starting with always 100;
 //Health is the same of stamna;
  //Speed load as 120000 and is unchangeable, what is extremelly wrong, as I should be able to increase 1000 of points each upgrade;
  //Defence = 0;
  //Damage = 1;

Even if I save their data manually, once I stop the game and run it again they NEVER load correctly, only when I load it by myself. Same trouble with equipments.

REGEDIT folder: alt text

If you need more details let me know!

regedit-prtscr.png (335.0 kB)
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 Igor_Vasiak · Apr 24, 2017 at 05:17 PM 0
Share

PS: I'm calling the Load () method once at LateUpdate, and then it works.

avatar image SohailBukhari · Apr 26, 2017 at 10:02 AM 0
Share

you are saving string in playerPrfes and you are using PlayerPrefs.SetInt(save_load.HeroName + gameObject.name + "Skill$$anonymous$$ey", theSkill);. you are setting string in integer, you can get playerprefs as you set. Use Correct format for saving playerprefs. If You are setting float in PlayerPrefs.SetInt then you can't get PlayerPrefs by PlayerPrefs.GetInt().

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by KukadiyaPrince · Apr 26, 2017 at 04:59 AM

 public class Score : MonoBehaviour
 {
          public int score = 20;
          void Start()
          {
                     PlayerPrefs.SetInt("Score",score); //Save score value
          }
        //Get Save value
         public void UpdateScore()
         {
                   int newScore = PlayerPrefs.GetInt("Score");
                  Debug.Log("Score" + newScore);
         }
 }
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 Igor_Vasiak · Apr 26, 2017 at 09:54 AM 0
Share

@$$anonymous$$ukadiyaPrince I do something like this, loading on Awake() and Update(only once using a bool, I added this after I posted the question, and it fixed my issues!) and I set the values at another function, Save(). I load the scripts calling a Load() method, from each script that needs to be loaded, at the Save_Load script. Example:

  public class Score: $$anonymous$$onoBehaviour
  {
       private int score;

       private Save_Load save_load;

       void LateUpdate ()
       {
            save_load = GetComponent<Save_Load>();
       }

       public void Save()
       {
            PlayerPrefs.SetInt(save_load.HeroName + "Score$$anonymous$$ey", score);
       }

       public void Load()
       {
            save_load = GetComponent<Save_Load>();
            score = PlayerPrefs.GetInt(save_load.HeroName + "Score$$anonymous$$ey", 0);
       }

  }

Now an example of the Save_Load script:

  public class Save_Load: $$anonymous$$onoBehaviour
  {
       private string heroName;
       public string HeroName { get { return heroName; } }

       private bool isLoaded;

       private Score score;
 
       void Awake ()
       {
            Load();
       }

       void LateUpdate ()
       {
            LoadScripts();
            if (!isLoaded) 
            {
                 Load();
                 isLoaded = true;
            }
       }

       void LoadScripts ()
       {
            score = GetComponent<Score>();
       }

       public void Save ()
       {
            score.Save();
       }

       public void Load ()
       {
            LoadScripts();
            heroName = gameObject.name;
            score.Load();
       }
  }

It's like this, just in a bigger scale, with a bunch of scripts at Save_Load, each one saving up to 6 values at the same time...

avatar image
0

Answer by anthot4 · Apr 28, 2017 at 05:48 AM

@Galahad_Hohan . Im not sure why it isn't working. If you use PlayerPrefs.SetInt etc it will save them automatically even if you quit the application and PlayerPrefs.GetInt will load it. My only thought is maybe you have placed them in the wrong function? Or something like that.

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

6 People are following this question.

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

Related Questions

How to get WebGL PlayerPref values out of Indexeddb? 0 Answers

PlayerPrefs: loading saved data from the menu? 1 Answer

Problems with saving/loading score with PlayerPrefs [C#] 1 Answer

[Solved] PlayerPrefs Vector 3 Defaulting to 0,0,0 1 Answer

Need help with using Player Prefs to save number of coins collected. 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