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 Chimera3D · Jun 08, 2012 at 02:08 AM · playerloadingsavingsystemprefs

Multiple Save (Saving & Loading System) With PlayerPrefs

Ok I have a single save, saving/loading system using player prefs and it works, but whenever I have a multiple saving/loading system, no matter what I try it won't work under any circumstances. It will save the values but whenever I try to play the game there is something wrong, the values get shared, switched, or it doesn't work at all. I've been trying to fix this for three days.

This is how the system is supposed to work. In the main menu you can press a button that will set the curGame1 or curGame2 int to 1 (0 means that this is not the active game, 1 means it is), it will also load the first level. In the first level there are two game objects with the saving/loading system code attached to a game object, when the scene loads one of the will delete themselves while the other is left to set the player's values (this was a last resort after EVERY other method failed). In the pause menu, when you leave the level to go back to the menu it sets the playerpref int "AlreadyPlayed[num]" to 1 if you played in that curGame. Once this is done the next time you load the scene, the variables will be set for you, therefore loading the variables from the last time you played. It's not that complex, but I do need to start commenting my code.

Please do not recommend something on the asset store to me, I'm learning how to do things myself, not how to buy stuff (I already know how to do that). Plus I'm only saving a few variables for now, not the entire state of the scene.

Here's the code I'm using (there are several):

The in-game saving/loading system

         var money : int;
         var player : GameObject;
         var reset : boolean;
         var playerVars : Roll;
         var ammo : Gun;
         var gun : GameObject;
         
         function Start () {
         
         if(PlayerPrefs.GetInt("CurGame1", 0) && PlayerPrefs.GetInt("CurGame2", 1)){
             Destroy (gameObject);
         }
         
         player = GameObject.Find ("player");
             
         gun = GameObject.Find ("gun");
         
         playerVars = player.GetComponent(Roll);
             
         ammo = discShoot.GetComponent(Gun);
         
             //Player prefs for money
             
             //Already played controls the alteration of health, shield, ect.
             if(PlayerPrefs.GetInt("alreadyPlayed1", 1)){
             
             money = PlayerPrefs.GetInt("Money1");
             
             playerVars.curHealth = PlayerPrefs.GetInt("Health1");
             
             playerVars.curShield = PlayerPrefs.GetInt("Shield1");
             
             ammo.ammo = PlayerPrefs.GetInt("Ammo1");
             
                 }else
             
              PlayerPrefs.SetInt("Health1", 100);
         
             PlayerPrefs.SetInt("Shield1", 100);
             
             PlayerPrefs.SetInt("Ammo1", 100);
         
         }
         
         function Update () {
             
             //Getting vars in them
         
             //Setting the ints
             PlayerPrefs.SetInt("Money1", money);
             
             PlayerPrefs.SetInt("Health1", playerVars.curHealth);
             
             PlayerPrefs.SetInt("Shield1", playerVars.curShield);
             
             PlayerPrefs.SetInt("Ammo1", ammo.ammo);
             
             
             //Temporary reset function (only works after you stop and start the game again)
             if(reset){
                 
                 PlayerPrefs.SetInt("alreadyPlayed1", 0);
                 
                 PlayerPrefs.SetInt("Money1", 0);
                 
                 money = 0;
                 
                 reset = false;
                 
             }    
         
         }

The loading menu code (the part that matters)

 if(GUILayout.Button(buttonString +1)){
         PlayerPrefs.SetInt("CurGame1", 1);
         PlayerPrefs.SetInt("CurGame2", 0);
         Application.LoadLevel("Ocean");
     }
     
     GUILayout.Space(spacing);
     
     if(GUILayout.Button(buttonString +2)){
         PlayerPrefs.SetInt("CurGame1", 0);
         PlayerPrefs.SetInt("CurGame2", 1);
         Application.LoadLevel("Ocean");
     }

Pause menu code (the part that matters)

                if(PlayerPrefs.GetInt("CurGame1", 1) && PlayerPrefs.GetInt("CurGame2", 0)){
         PlayerPrefs.SetInt("AlreadyPlayed1", 1);
         }else
         
         if(PlayerPrefs.GetInt("CurGame1", 0) && PlayerPrefs.GetInt("CurGame2", 1)){
         PlayerPrefs.SetInt("AlreadyPlayed2", 1);
         }
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
0
Best Answer

Answer by Chimera3D · Jun 08, 2012 at 03:26 AM

I fail, I wasn't using the code correctly. I fixed it. I didn't realize I had to get the int of the PlayerPrefs then compare that to the 1 or 0.

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
2

Answer by rmele09 · Aug 27, 2013 at 05:39 PM

could you repost the final code so we can see what you fixed?

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Saving with PlayerPrefs? How it works? 1 Answer

Where does unity save player preds when testing in the editor? 2 Answers

Where is PlayerPrefs Located on Windows? 2 Answers

PlayerPrefs - Sound slider values 1 Answer

How to consume input when automatically switching control schemes through a Player Input Component 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