- Home /
Question by
zak666 · Jan 06, 2017 at 09:06 AM ·
savepersistence
MAC - Hey Gang trying to create a Save file, what am I doing wrong here? FileNotFoundException: Could not find file "/Users/zak/Library/Application Support/M2H/PhotonUnity/gameInfo.dat"
Trying to create and load a save game file for my game however I'm running into an error: FileNotFoundException: Could not find file "/Users/zak/Library/Application Support/M2H/PhotonUnity/gameInfo.dat"
Also will a fix also work on windows builds?
using UnityEngine;
using System.Collections;
using System;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using UnityEngine.UI;
public class SaveSystem : MonoBehaviour {
public GameObject Loggedin;
public GameObject LoggedOut;
public GameObject NoUsername;
public GameObject MissionControll;
public Text Username;
public PlayerData dataP;
public PlayerStats Stats;
public void Start () {
Stats = MissionControll.gameObject.GetComponent<PlayerStats> ();
}
public void Save() {
if (Username != null) {
dataP.Username = Username.text.ToString ();
BinaryFormatter bf = new BinaryFormatter ();
FileStream file = File.Open (Application.persistentDataPath + "/gameInfo.dat",
FileMode.Open);
PlayerData data = new PlayerData ();
data.XP = Stats.Xp;
data.SG = Stats.SG;
data.RS = Stats.RS;
data.LVL = Stats.LVL;
//small ships
data.Ship01 = Stats.Ship01;
data.Ship02 = Stats.Ship02;
data.Ship03 = Stats.Ship03;
data.Ship04 = Stats.Ship04;
data.Ship05 = Stats.Ship05;
data.Ship06 = Stats.Ship06;
//medium ships
data.Ship001 = Stats.Ship001;
data.Ship002 = Stats.Ship002;
//heavy ships
data.Ship0001 = Stats.Ship0001;
data.Ship0002 = Stats.Ship0002;
//Capital ships
data.Ship00001 = Stats.Ship00001;
data.Ship00002 = Stats.Ship00002;
//Science Ships
data.Ship000001 = Stats.Ship000001;
data.Ship000002 = Stats.Ship000002;
data.Ship000003 = Stats.Ship000003;
data.Ship000004 = Stats.Ship000004;
//Skills Russian
data.SkillsR01 = Stats.SkillsR01;
data.SkillsR02 = Stats.SkillsR02;
data.SkillsR03 = Stats.SkillsR03;
data.SkillsR04 = Stats.SkillsR04;
data.SkillsR05 = Stats.SkillsR05;
//Skills American
data.SkillsA01 = Stats.SkillsA01;
data.SkillsA02 = Stats.SkillsA02;
data.SkillsA03 = Stats.SkillsA03;
data.SkillsA04 = Stats.SkillsA04;
data.SkillsA05 = Stats.SkillsA05;
bf.Serialize (file, data);
file.Close ();
}
NoUsername.SetActive (true);
}
public void Load() {
if(File.Exists(Application.persistentDataPath + "/gameInfo.dat")) {
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + "/gameInfo.dat",
FileMode.Open);
PlayerData data = (PlayerData)bf.Deserialize(file);
file.Close();
Stats.Username = data.Username;
Stats.Xp = data.XP;
Stats.SG = data.SG;
Stats.RS = data.RS;
Stats.LVL = data.LVL;
//small ships
Stats.Ship01 = data.Ship01;
Stats.Ship02 = data.Ship02;
Stats.Ship03 = data.Ship03;
Stats.Ship04 = data.Ship04;
Stats.Ship05 = data.Ship05;
Stats.Ship06 = data.Ship06;
//medium ships
Stats.Ship001 = data.Ship001;
Stats.Ship002 = data.Ship002;
//heavy ships
Stats.Ship0001 = data.Ship0001;
Stats.Ship0002 = data.Ship0002;
//Capital ships
Stats.Ship00001 = data.Ship00001;
Stats.Ship00002 = data.Ship00002;
//Science Ships
Stats.Ship000001 = data.Ship000001;
Stats.Ship000002 = data.Ship000002;
Stats.Ship000003 = data.Ship000003;
Stats.Ship000004 = data.Ship000004;
//Skills Russian
Stats.SkillsR01 = data.SkillsR01;
Stats.SkillsR02 = data.SkillsR02;
Stats.SkillsR03 = data.SkillsR03;
Stats.SkillsR04 = data.SkillsR04;
Stats.SkillsR05 = data.SkillsR05;
//Skills American
Stats.SkillsA01 = data.SkillsA01;
Stats.SkillsA02 = data.SkillsA02;
Stats.SkillsA03 = data.SkillsA03;
Stats.SkillsA04 = data.SkillsA04;
Stats.SkillsA05 = data.SkillsA05;
LoggedOut.SetActive (false);
Loggedin.SetActive (true);
}
}
}
[Serializable]
public class PlayerData {
// TODO: see about making gets and sets.
// TODO: automate the generation of this data structure. See:
// http://forums.devx.com/showthread.php?170650-How-to-dynamically-add-property-to
// Add new variables for loading and saving here.
public string Username;
public int XP;
public int LVL;
public int SG;
public int RS;
public int Ship01;
public int Ship02;
public int Ship03;
public int Ship04;
public int Ship05;
public int Ship06;
public int Ship001;
public int Ship002;
public int Ship0001;
public int Ship0002;
public int Ship0003;
public int Ship0004;
public int Ship00001;
public int Ship00002;
public int Ship000001;
public int Ship000002;
public int Ship000003;
public int Ship000004;
public int SkillsA01;
public int SkillsA02;
public int SkillsA03;
public int SkillsA04;
public int SkillsA05;
public int SkillsR01;
public int SkillsR02;
public int SkillsR03;
public int SkillsR04;
public int SkillsR05;
}
Comment
Answer by KoenigX3 · Jan 06, 2017 at 04:00 PM
The saving function does not check if the 'gameInfo.dat' file exists or not. I assume, that you have not created the file in the first place.
You should use these lines:
FileStream file;
if(File.Exists(Application.persistentDataPath + "/gameInfo.dat")) file = File.OpenWrite(Application.persistentDataPath + "/gameInfo.dat");
else file = File.Create(Application.persistentDataPath + "/gameInfo.dat");
Your answer
![](https://koobas.hobune.stream/wayback/20220612104209im_/https://answers.unity.com/themes/thub/images/avi.jpg)