- Home /
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!
Protip: You don't want PlayerPrefs.Save() in an Update $$anonymous$$ethod, that's nuts (saving once per frame...bah!)
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
and you should do this in Start too, not in Update
if (Progress == 0) {
Progress = 1;
}
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.
you still ignore my answer.
you dont store value read by playerpref.getint in your Progress variable.
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!
Oh now i see it! I misunderstood what you said! I did not saw that line of code you wrote! Thanks for helping!