Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How do you save a boolean to PlayerPrefs?

Discussion in 'Editor & General Support' started by thylaxene, Jul 6, 2007.

  1. thylaxene

    thylaxene

    Joined:
    Oct 10, 2005
    Posts:
    716
    How do you save a boolean to PlayerPrefs? and load it back in again? I naively tried just saving my boolean to PlayerPrefs.SetInt hoping it would just work (you know the whole 1, 0 thing)... but no, strangely I got a casting error. :wink:

    So can anyone enlighten me on how this is done? Or do I just need to shut up and write a nice little conversion function?

    Cheers.
     
    AlanMattano and mutazsh like this.
  2. NCarter

    NCarter

    Joined:
    Sep 3, 2005
    Posts:
    686
    This should do it:

    Code (csharp):
    1. PlayerPrefs.SetInt("Name", (yourBool ? 1 : 0));
    2. yourBool = (PlayerPrefs.GetInt("Name") != 0);
     
  3. thylaxene

    thylaxene

    Joined:
    Oct 10, 2005
    Posts:
    716
    ah! I knew there would be a little bit of coding magic to do it! :wink:

    Cheers.
     
  4. Bleackk

    Bleackk

    Joined:
    Feb 7, 2016
    Posts:
    6
    You do understand sir that this solution just saved a few thousand people a good 10 hours worth of F*cking aroudn with the code? xD
     
  5. fausto-mourato

    fausto-mourato

    Joined:
    Jun 18, 2015
    Posts:
    4
    Or you can use the class Convert, like this:

    Code (csharp):
    1. PlayerPrefs.SetInt("Name", Convert.ToInt32(yourBool));
    2. yourBool = Convert.ToBoolean(PlayerPrefs.GetInt("Name"));
    Your choice.
     
  6. Deleted User

    Deleted User

    Guest

    Is there any overhead with using the Covert class from System?
     
  7. kaisersam

    kaisersam

    Joined:
    Mar 31, 2016
    Posts:
    39
    Is it possible to explain the code quickly?
     
  8. Lili_Chan

    Lili_Chan

    Joined:
    May 13, 2018
    Posts:
    4
    @kaisersam Sorry, I'm a little late to the party.
    That's actually very rudimentary.

    yourBool is a boolean.
    Code (CSharp):
    1. int value = yourBool ? 1 : 0;
    is like writing
    Code (CSharp):
    1. int value;
    2. if (yourBool)
    3. {
    4.      value = 1;
    5. }
    6. else
    7. {
    8.      value = 0;
    9. }
    It actually evaluates yourBool. If yourBool is true, then use the first value (before the colon), if it is false, then use the value after the colon.
    yourBool ? 1 : 0 -> is yourBool true ? True : False

    Ok, so. You set the key "Name" of the playerprefs, which is an int, to the value 1 or 0, depending wether yourBool is true or not.
    To retrieve the value from the int key, and assign it to a boolean, you use the second line by checking: "is the value of "Name" not zero ?" If it is not zero, it return true, otherwise it returns false. So yourBool is equal to true (1) if "Name" is not equal to zero, and is equal to false (0) if "Name" is equal to 0.

    All of that makes sense:
    If yourBool is true (1), you store a value of 1 in "Name".
    If yourBool is false (0), you store a value of 0 in "Name".
    If "Name" is not zero (then it must be 1), yourBool is set to true.
    If "Name" is zero, yourBool is set to false.

    Hope it helps.
     
  9. kaisersam

    kaisersam

    Joined:
    Mar 31, 2016
    Posts:
    39
    Thanks a lot for sharing those tips!
     
  10. gfsoft03

    gfsoft03

    Joined:
    Oct 23, 2019
    Posts:
    1
    Thanks a lot for your answers guys
     
  11. amoraleite

    amoraleite

    Joined:
    Oct 16, 2014
    Posts:
    41
    Thank you guys! Help me a lot!
     
  12. Giannigiardinelli

    Giannigiardinelli

    Joined:
    May 8, 2014
    Posts:
    179
    Thx you guys !
     
    divyeshdonga likes this.
  13. hungiezilla

    hungiezilla

    Joined:
    Sep 26, 2022
    Posts:
    2
    Crazy how this is still helping people 16 years later, so glad this forum is still up.
     
    vrishank0628, Amedo579 and thylaxene like this.