Search Unity

  1. Looking for a job or to hire someone for a project? Check out the re-opened job forums.
    Dismiss Notice
  2. Unity 2020 LTS & Unity 2021.1 have been released.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    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:
    713
    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.
     
    mutazsh likes 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:
    713
    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.
     
    BonusB, Lexudau and SteveHinan like this.
  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:
    38
    Is it possible to explain the code quickly?
     
  8. Ylliana

    Ylliana

    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.
     
    Ruslank100, BonusB, gfsoft03 and 5 others like this.
  9. kaisersam

    kaisersam

    Joined:
    Mar 31, 2016
    Posts:
    38
    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:
    27
    Thank you guys! Help me a lot!
     
unityunity