Search Unity

My Saving data when exiting app code does not work

Discussion in 'Scripting' started by Lim_sihun, Jun 19, 2018.

  1. Lim_sihun

    Lim_sihun

    Joined:
    Jun 19, 2018
    Posts:
    1
    public void OnExitAppDown()
    {
    BinaryFormatter bf = new BinaryFormatter();
    FileStream file = File.Open(Application.persistentDataPath + "/playerInfo.dat", FileMode.Open);
    PlayerData pdata = new PlayerData();
    pdata.money = this.money;
    pdata.moneyIncreasement = this.moneyIncreasement;
    pdata.reviverCount = PlayerPrefs.GetInt("reviverCount");
    pdata.reviverPercentage = PlayerPrefs.GetInt("reviverPercentage");
    for (int i = 0; i < 4; ++i)
    {
    pdata.Bed = PlayerPrefs.GetInt("Bed" + i);
    pdata.Reviver = PlayerPrefs.GetInt("Reviver" + i);
    pdata.Sofa = this.Sofa;
    pdata.Bookshelf = this.Bookshelf;
    }
    bf.Serialize(file, pdata);
    file.Close();

    Application.Quit();
    }

    this is the problem code.
    this isn't a import problem, the code runs perfectly fine. but when I press the exitAppButton, It doesn't work... the game runs perfectly fine without exiting...
    help me... I have to submit this app by tomorrow...
     
  2. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,089
    Don't use File.Open unless you change the FileMode to FileMode.OpenOrCreate. Use File.Create instead.