[C#]Can't get Playerprefs to work

Hello, i’ve been trying to figure the playerprefs for a day now but no success even tho i did look some videos about them but i still didn’t understand how exactly to do it.

I have a current exp and a level variable i want to save but i can’t get it to work
it saves perfectly fine but it doesn’t load it right, so i guess i might do something wrong with loading it.

Could some give a example how i would do that?

Maybe this could help to see what i did wrong http://pastebin.com/SVkkcb01

The code looks ok to me.
Have you tried debugging out the result after you load the values?
How is PlayerStats.curExp implemented?

Pretty much what ThermalFusion said. This script itself looks fine to me. Where did you call on these functions, or should I say, did you make sure you called on them? You can throw in someting like Debug.Log or print to give you a message if it actually goes through, then work from there.

Yeah, it debugs fine, weird it doesn’t work. Could it be because the curExp is static? Not sure why it wouldn’t work if it is static because it seems to work for others even if it is static. I use Daikon Forge Gui so i make a event binding on the Buttons to call the function(s)

Did you try printing out curExp at different times? And you made sure both ends are going through- putting a print/debug.log in the save/set function AND in the get function?

Alright so now i added the debug.log in all of them it saves fine
but when i load the level it gets it but it is 0. I think it is weird it saves fine but not loads/gets it fine

Did you check the values of PlayerStats.curExp and PlayerStats.maxExp at save and load time? Also, they’re integers, correct?

Yeah they integers, and what you mean with if i check the values?

print(PlayerStats.curExp) and print(PlayerStats.maxExp) at both saving and loading. It may be a problem with PlayerStats itself.

Oh yeah that is what i did at the saving and loading and i get those values i told you , just with debug.log

EDIT:

Alright so maxExp is also 0 for some reason, so there must be something completely wrong
But when save it is curExp is 60 and maxExp is 400

Yeah… just realized. You’re loading the level before loading the values. Basically, the load occurs first and thus it wipes out the objects- including the one that it’s on and the instructions after.

Basically, you can create a temporary (or permanent, but that gets trickier since you’d have to make sure there aren’t any copies) object that gets instantiated before you load a level, has the DontDestroyOnLoad property on it, saves the data, and DESTROYS itself once its done for no memory leaks. I use this technique for a fading technique at the moment as well as saving. Loading I handle during the start of the game on a central object.

My suggestion: First try to see if you can load the values at all by throwing the GetInt part straight into some random Start() function as a test.

Even in the start function it is 0 on both when loaded

but save it works fine

Can you post the other script that calls on those functions? It would save a bunch of guess work :wink:
And when you mean even in the start function, it’s like:

void Start(){
    PlayerStats.curExp = PlayerPrefs.GetInt ("cur_exp");
    PlayerStats.maxExp = PlayerPrefs.GetInt ("max_exp");
}

I guess I’ll also need the definition of PlayerPrefs just in case…

I’m hoping that if we can’t see an error soon, someone else can step in. Because the next step would be to ask for (relevant pieces of) the project, which would be a bit of trouble for all of us. :wink:

No scripts is really calling the functions, only the Daikon Forge GUI event binding is calling it when clicked.
And yeah it looks like that in the start function.
But this is how it looks in the PlayerStats

public static int curExp;
public static int maxExp;

Those are the variables.

This is from the MainMenu.cs

public void Continue()
	{
		//PauseMain pm = gameObject.GetComponent<PauseMain> ().player;
		PlayerStats.curExp = PlayerPrefs.GetInt ("curExp");
		PlayerStats.maxExp = PlayerPrefs.GetInt ("maxExp");
		Debug.Log (PlayerStats.curExp);
		Debug.Log (PlayerStats.maxExp);
		Application.LoadLevel ("Game");

	}

Tho i did try to put it in the start function in the PlayerStats, without the Application.LoadLevel of course and without the

PlayerStats.curExp
So it looked like this

curExp = PlayerPrefs.GetInt ("curExp");

The same for the others, this is probably confusing :stuck_out_tongue: But it’s hard to explain stuff

…That last line was with PlayerStats.curExp, right? I mean, WITH the prefix?
Also, any errors?

And I suggest use some simple setups to see if it works at all. So, basically, click this key to save and click this key to load. Then go from there- that way, you remove as many “ifs” as possible from the situation.

Honestly, I’m not even sure. I need another pair of eyes, and I’m hoping that someone else comes by to look at this… I can tell you that it’s not a problem with the static variables but most likely how it’s set up, so adding what I like to call “cheat keys” to test it out quickly would help.

Yeah all were with the prefix but i changed it :stuck_out_tongue: But i try the onkey press load/save

EDIT: Alright i did the keys thing but it still doesn’t load it :stuck_out_tongue: