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.
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
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.
Can you post the other script that calls on those functions? It would save a bunch of guess work
And when you mean even in the start function, it’s like:
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.
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;
…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.