- Home /
Saving a variable?
I've done some research and I've decided that I need to use playerprefs but I'm still confused. I have a variable called "PlayerXP", or "RoomMultiplayerMenu.PlayerXP" and I need to save it. So would I say PlayerXP = PlayerPrefs.GetInt(RoomMultiplayerMenu.PlayerXP");
? even though I already have that variable set up? And then when loading could I just say 'RoomMultiplayerMenu.PlayerXP = PlayerXP.toString;'. And if RoomMultiplayerMenu.PlayerXP doesn't exist how do you tell it "else RoomMultiplayerMenu.PlayerXP = 0;" or something?
Answer by hav_ngs_ru · Jan 21, 2015 at 01:49 PM
So would I say PlayerXP = PlayerPrefs.GetInt(RoomMultiplayerMenu.PlayerXP");? even though I already have that variable set up?
you trying to read value this way. To save it you should use SetInt. the fact that you already have this variable is not related to saving playerprefs. by the way, you forgot a doublequote.
PlayerPrefs.SetInt("RoomMultiplayerMenu.PlayerXP", PlayerXP);
PlayerPrefs.Save();
And then when loading could I just say 'RoomMultiplayerMenu.PlayerXP = PlayerXP.toString;'.
No. You should read it explicity
PlayerXP = PlayerPrefs.GetInt("RoomMultiplayerMenu.PlayerXP");
And if RoomMultiplayerMenu.PlayerXP doesn't exist how do you tell it "else RoomMultiplayerMenu.PlayerXP = 0;" or something?
GetInt returns 0 if var doesnt exist by default, without extra parameters. But if you want have smth else when pref doesnt exist (e.g. 1) - use defaultValue parameter:
PlayerXP = PlayerPrefs.GetInt("RoomMultiplayerMenu.PlayerXP", 1);