Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Saving and Loading bool variables using PlayerPrefs

Discussion in 'Scripting' started by jevanswow, Jun 6, 2015.

  1. jevanswow

    jevanswow

    Joined:
    Apr 17, 2015
    Posts:
    33
    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
     
  2. larku

    larku

    Joined:
    Mar 14, 2013
    Posts:
    1,422
    Code (csharp):
    1. bool myBool = false;
    2.  
    3. // save as 1 if true, 0 otherwise
    4. PlayerPrefs.SetInt("somebool", myBool ? 1 : 0);
    5.  
    6. // if is 1 then is true, false otherwise.
    7. myBool = PlayerPrefs.GetInt("somebool") == 1 ? true : false;
     
  3. jevanswow

    jevanswow

    Joined:
    Apr 17, 2015
    Posts:
    33
    Thank you, that'll save me the time of having to go in there and changing the bool to int
     
  4. jevanswow

    jevanswow

    Joined:
    Apr 17, 2015
    Posts:
    33
    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 :)
     
    larku likes this.