Search Unity

Help with PlayerPrefs

Discussion in 'Editor & General Support' started by gustavozor, Aug 18, 2019.

  1. gustavozor

    gustavozor

    Joined:
    Oct 8, 2018
    Posts:
    2
    Hello guys.. I'm trying to save boolean values for some items in my game which only features are "you have it or you dont have it" = true, false
    The items are: air, water, fire and earth gems
    The problem is, I can save their values with "SetInt" using the code below
    But how can I load the info?

    here is my code

    Code (CSharp):
    1. public void Save()
    2.     {
    3.         water = Controller.GetComponent<Manager>().waterGem;
    4.         air = Controller.GetComponent<Manager>().airGem;
    5.         fire = Controller.GetComponent<Manager>().fireGem;
    6.         earth = Controller.GetComponent<Manager>().earthGem;
    7.         saved = Controller.GetComponent<Manager>().hasSaved;
    8.         PlayerPrefs.SetInt("WaterGem", (water ? 1 : 0));
    9.         PlayerPrefs.SetInt("AirGem", (air ? 1 : 0));
    10.         PlayerPrefs.SetInt("EarthGem", (earth ? 1 : 0));
    11.         PlayerPrefs.SetInt("FireGem", (fire ? 1 : 0));
    12.  
    13. public void Load()
    14.     {
    15.         water = PlayerPrefs.GetInt("WaterGem", false);
    16.         Controller.GetComponent<Manager>().waterGem = water;
    17.         air = PlayerPrefs.GetInt("AirGem", false);
    18.         Controller.GetComponent<Manager>().airGem = air;
    19.         earth = PlayerPrefs.GetInt("EarthGem", false);
    20.         Controller.GetComponent<Manager>().earthGem = earth;
    21.         fire = PlayerPrefs.GetInt("FireGem", false);
    22.         Controller.GetComponent<Manager>().fireGem = fire;
    23.     }
    24.    
    the "load" function is definitely not working
    please heeeeelp
     
  2. SoftwareGeezers

    SoftwareGeezers

    Joined:
    Jun 22, 2013
    Posts:
    902
    You are loading an int as a bool. GetInt is going to return a number which you need to turn into a boolean.

    Code (CSharp):
    1. if(PlayerPrefs.GetInt("WaterGem", 0) == 1)
    2.     water = true;
    3. }else{
    4.    water = false;
    5. };
    Or in shorthand, water = PlayerPrefs.GetInt("WaterGem", 0) == 1;
     
    gustavozor likes this.
  3. gustavozor

    gustavozor

    Joined:
    Oct 8, 2018
    Posts:
    2
    thank you so much! it worked