Saving and Loading bool variables using PlayerPrefs

Hi everyone

Does anyone know the code or at the very least the documentation on saving and loading bool variables? I know of the int/string/float but unsure on how to use the player prefs for bool.

Also the game I’m developing, I wouldn’t say it’s big in the sense of Oblivion or anything but the more I script and put in the more I realize I have to actually save/load. For example part of the game in the beginning is where the player talks to a mayor and chooses a class, once he/she chooses a class they then talk to warrior master/trainer and does what is required and then gets promoted to soldier. I got the basics in there but unfortunately even though I save the game after the player becomes a soldier, when a player exits and then loads the mayor asks the player to choose a class again, so I guess the question I have, is there an easier or quicker way to ensuring the values in those variables will stay the same. Like a code in a script that’ll save all the necessary variables instead of having to put each individual one in separately.

Thanks

bool myBool = false;

// save as 1 if true, 0 otherwise
PlayerPrefs.SetInt("somebool", myBool ? 1 : 0);

// if is 1 then is true, false otherwise.
myBool = PlayerPrefs.GetInt("somebool") == 1 ? true : false;

Thank you, that’ll save me the time of having to go in there and changing the bool to int

I tested the one for the chose class from the mayor and it works like a charm. I appreciate the help greatly and thank you very much :slight_smile:

1 Like