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 Thijsx7 · Dec 30, 2014 at 12:47 PM · c#playerprefssave

PlayerPrefs don't work

Hi everybody! I am making a platformer game where the player has to walk to a finish line. I want to save the progress of the game so you don't have to start over the next time you play, but it doesn't save! I want it to save the current level where the player is in. Here is the script i made for the second level:

     using UnityEngine;
     using System.Collections;
     
     public class Scene2Manager : MonoBehaviour {
     public GameObject Player;
     public int Currentlevel;
     // Use this for initialization
 
     void Start () {
         Instantiate (Player);
         Currentlevel = Application.loadedLevel;
     }
         
     // Update is called once per frame
     void Update () {
         Progress();
     }
     void Progress(){
         PlayerPrefs.SetInt("Level Progress", Currentlevel);
         PlayerPrefs.Save();
     }
 }

So at the start of the second level it saves it. Then when i let the player die and return to the main menu, it should load the second level when you click play. There is no error but it just starts at the first level! Here is the code for the main menu:

 using UnityEngine;
 using System.Collections;
 
 public class StartScreenManager : MonoBehaviour {
     public int Progress;
     // Use this for initialization
     void Start () {
         PlayerPrefs.GetInt ("Level Progress", Progress);
     }
     
     // Update is called once per frame
     void Update () {
         if (Progress == 0) {
             Progress = 1;        
         }
     }
     public void WhenClickExit(){
 
         Application.Quit();
             
 
     }
     public void WhenClickPlay(){
         Application.LoadLevel (Progress);
     }
 }

I hope you guys can help me out!

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 Landern · Dec 30, 2014 at 12:46 PM 2
Share

Protip: You don't want PlayerPrefs.Save() in an Update $$anonymous$$ethod, that's nuts (saving once per frame...bah!)

2 Replies

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

Answer by hav_ngs_ru · Dec 30, 2014 at 01:05 PM

 void Start () {
   // PlayerPrefs.GetInt ("Level Progress", Progress); // this is wrong!
   Progress = PlayerPrefs.GetInt ("Level Progress"); // this is right
 }

And as Landern said - dont save once per frame!! save at Start or any else method called once per level, not per frame

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 hav_ngs_ru · Dec 30, 2014 at 01:06 PM 0
Share

and you should do this in Start too, not in Update

      if (Progress == 0) {
       Progress = 1;
      }

avatar image
0

Answer by Thijsx7 · Dec 30, 2014 at 08:00 PM

I did what you asked me but I still have the same problem! When I click on the play button the game still loads level 1! Here is the scripts that I have on this moment: The Menu:

 using UnityEngine;
 using System.Collections;
 
 public class StartScreenManager : MonoBehaviour {
     public int Progress;
     // Use this for initialization
     void Start () {
         PlayerPrefs.GetInt ("Level Progress", Progress);
         if (Progress == 0) {
             Progress = 1;        
         }
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 
     public void WhenClickExit(){
 
         Application.Quit();
             
 
     }
 
     public void WhenClickPlay(){
         Application.LoadLevel (Progress);
     }
 }

And the manager:

 using UnityEngine;
 using System.Collections;
 
 public class Scene2Manager : MonoBehaviour {
     public GameObject Player;
     public int Currentlevel;
     // Use this for initialization
     void Start () {
         Instantiate (Player);
         Currentlevel = Application.loadedLevel;
         Progress();
     }
     
     // Update is called once per frame
     void Update () {
 
     }
     void Progress(){
         PlayerPrefs.SetInt("Level Progress", Currentlevel);
         PlayerPrefs.Save();
     }
 }
 
 

I think that I did something wrong, but still no error.

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 hav_ngs_ru · Dec 30, 2014 at 10:18 PM 1
Share

you still ignore my answer.

you dont store value read by playerpref.getint in your Progress variable.

avatar image Thijsx7 · Dec 30, 2014 at 11:12 PM 0
Share

I don't understand you. I am a bit new to Unity so i have much to learn. I think I did everything you said. I put the things you mentioned in the start method. I hope that you can help me. Thanks!

avatar image Thijsx7 · Dec 30, 2014 at 11:15 PM 0
Share

Oh now i see it! I misunderstood what you said! I did not saw that line of code you wrote! Thanks for helping!

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

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

Save player position when trigger is destroyed 1 Answer

Can anyone help me figure out why PlayerPrefs wont save the money value?,Can anyone help me as to why my money won't save in my game? Using PlayerPrefs 1 Answer

How to make a Saved/Favorites system in unity? 0 Answers

Android Build not deleting PlayerPrefs 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