I’m having trouble with Player Prefs. When i save game and click to load with the game running, the scene is loaded correctly. When i stop previewing game and start again, i click in the load button and it doesn’t open anything. Is it common?
For further information, the script:
using UnityEngine;
using System.Collections;
public class geral : MonoBehaviour {
public static int getlvl;
private pinguim anyname;
public GameObject goFindScript;
public static int trying_saved;
void Load()
{
anyname = goFindScript.GetComponent<pinguim>();
trying_saved = pinguim.tentativas;
PlayerPrefs.GetInt("getlvl");
PlayerPrefs.GetInt("trying_saved");
PlayerPrefs.Save();
}
}
The code to load:
using UnityEngine;
using System.Collections;
public class menu : MonoBehaviour {
public Texture load;
public Texture newgame;
public Texture quit;
public Texture logo;
private pinguim anyname;
private geral recebe;
public int trying;
public GameObject goFindScript;
void Start()
{
anyname = goFindScript.GetComponent<pinguim>();
recebe = goFindScript.GetComponent<geral>();
}
void OnGUI () {
GUI.DrawTexture(new Rect(25, 20, 145, 20), logo);
if (GUI.Button(new Rect(25, 100, 120, 25), load))
{
PlayerPrefs.SetInt("geral.getlvl", geral.getlvl);
PlayerPrefs.SetInt("geral.trying_saved", geral.trying_saved);
pinguim.tentativas = geral.trying_saved;
Hve you set PlayerPrefs.DeleteAll(); anywhere?
Edit.
Also, I noticed that you have used Save() after you Get values and not when you Set values. This seems like backwards logic to me.
No i didn’t used DeleteAll();. So the problem is in the Save()?
Last time I wrote code in portuguese it backfired on me when I hit the thousand lines of code mark. Might want to write your code in english, since c# itself is in english.
Basically, you’re not loading your int from the PlayerPrefs anywhere, since the return of GetInt is not being stored… anywhere. Also, the posted code shows that you’re saving in player prefs with one key, but loading with other key - and this will not work. The keys need to be the same.
load stuff
int someValue = PlayerPrefs.GetInt("Saved value");
save stuff
PlayerPrefs.SetInt("Saved value", someValue);
Your saving with the key “geral.getlvl” but your loading with “getlvl”.
Use the same key and it should be fixed!
A question: I notice that the code Application.Quit(); doesn’t stop previewing game in Unity, and it’s the code that make PlayerPrefs be saved. Maybe the PlayerPrefs Works correctly if i export game?
Also i fixed all errors reported above.
In the time it took you to post you could have tried it yourself.
Yes - Quit does nothing in the Editor. Yes - Quit will save player prefs. No - it is not the only way to save player prefs, that’s what the Save() method is for.
Yes american, but my game is for WP8 and i haven’t enough RAM to load the emulator
So you’re expecting the forum to debug your code for you? How are you developing a game for a platform that you can’t test? Also - in terms of PlayerPrefs, the platform shouldn’t matter.