Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Not saving files!!!

Discussion in 'Android' started by DukeNuke3m, Sep 8, 2017.

  1. DukeNuke3m

    DukeNuke3m

    Joined:
    Apr 28, 2016
    Posts:
    21
    Android doesnt save my files,so everytime i open game again,it resets.I tested on few devices and all have the same mistake.Please help,here is code,the load and save function is called trough other scripts on the end of every level!

    using System.Collections.Generic;
    using UnityEngine;
    using System.Runtime.Serialization.Formatters.Binary;
    using System.IO;


    [System.Serializable]
    class PlayerData
    {
    public int level1 = 0;
    public int level2 = 0;
    public int level3 = 0;
    }
    public class GameManager : MonoBehaviour {

    public static GameManager manager;
    public int level1=0;
    public int level2=0;
    public int level3=0;
    // Use this for initialization
    void Awake () {
    if (manager == null) {
    DontDestroyOnLoad (gameObject);
    } else if (manager != this) {
    Destroy (gameObject);
    }
    Load ();

    }
    public void Save()
    {
    BinaryFormatter bf = new BinaryFormatter();
    FileStream file = File.Create(Application.persistentDataPath + "/playerInfo.dat");

    PlayerData data = new PlayerData ();
    data.level1 = level1;
    data.level2 = level2;
    data.level3 = level3;
    bf.Serialize (file, data);
    file.Close ();
    }
    public void Load()
    {
    Debug.Log ("ItsWorking");
    if (File.Exists (Application.persistentDataPath + "/playerInfo,dat")) {
    BinaryFormatter bf = new BinaryFormatter ();
    FileStream file = File.Open (Application.persistentDataPath + "/playerInfo.dat", FileMode.Open);
    PlayerData data = (PlayerData)bf.Deserialize (file);
    file.Close ();

    level1 = data.level1;
    level2 = data.level2;
    level3 = data.level3;
    Debug.Log ("ALl is WORKING");
    }
    }
    }
     
  2. DukeNuke3m

    DukeNuke3m

    Joined:
    Apr 28, 2016
    Posts:
    21
    There is an error on editor saying that path for loading doesnt exists
     
  3. guzzo

    guzzo

    Joined:
    Feb 20, 2014
    Posts:
    79
    You mistyped the file name. You are saving as "playerInfo.dat" and asking for a "playerInfo,dat"
    Code (csharp):
    1.  if (File.Exists (Application.persistentDataPath + "/playerInfo,dat")) {