Search Unity

Saving a bool between scenes?

Discussion in 'Scripting' started by Erin abrams, May 30, 2015.

  1. Erin abrams

    Erin abrams

    Joined:
    Mar 4, 2015
    Posts:
    22
    To save integers and numbers through scenes I was using playerprefs but sadly there is no playerprefs.setbool. so I was wondering how I might go about saving a bool between scenes in c#.
     
  2. pauloaguiar

    pauloaguiar

    Joined:
    May 13, 2009
    Posts:
    700
    You can use "int" = 0 or 1. that mean if 0 is nothing else if 1 that mean something.
    using 0 it can mean false and 1 can be true.
     
    art7pro likes this.
  3. Erin abrams

    Erin abrams

    Joined:
    Mar 4, 2015
    Posts:
    22
    Do you know how I might do it by using (dontdestroyonload)
     
  4. elmar1028

    elmar1028

    Joined:
    Nov 21, 2013
    Posts:
    2,359
    Add "static" to your variable. It will keep the values the same if your game changes scene or restarts it.

    So in this case if you have this:
    Code (csharp):
    1. public int number = 0;
    Do this:
    Code (csharp):
    1. public static int number = 0;
    Note, it won't keep the values after you restart the game entirely.
    You can't have several static variables with same name, regardless if you declare them in different scripts.
     
  5. Erin abrams

    Erin abrams

    Joined:
    Mar 4, 2015
    Posts:
    22
    If I were to do something like this for a mobile game. If the user were to close and re-open the game would that bool be saved?
     
  6. GreenBoxInteractive

    GreenBoxInteractive

    Joined:
    Mar 23, 2015
    Posts:
    135
    You could always use PlayerPrefs and use 0 and 1 as a bool.
     
  7. elmar1028

    elmar1028

    Joined:
    Nov 21, 2013
    Posts:
    2,359
    For static variables. No, as noted earlier. PlayerPrefs() is your best option if you want to save data.

    Just note that PlayerPrefs can be easily compromised by skilled players by reverse engineering your compiled game, but I don't think that would be an issue.