- Home /
Binary Serialization (In Editor) - Path Access denied / File not found
Hey fellow developers,
I am trying to use common Serialiation to save and load data in a file. I've used this method countless times and it worked flawlessly every time. But in this project, for some reason, it does not work.
What may be important: I am trying to save and load in EDITOR, so NOT at runtime. Does that cause any issues? The save data is a class LevelData that contains a list of TrafficScene. TrafficScene holds ints, strings and a SceneAsset. Does the SceneAsset type cause any issues? Both classes are marked as Serializable. Both do not inherit from anything. I am either getting a Path Access Denied error or a File not found error. Here are my save and load methods:
public void LoadLevelData()
{
FileStream file = null;
try
{
BinaryFormatter bf = new BinaryFormatter();
file = File.Open(Application.dataPath + data_Path, FileMode.Open);
levelData = (LevelData)bf.Deserialize(file);
if (levelData != null)
{
levels = levelData.Levels;
}
Debug.Log("Successfully loaded");
}
catch (Exception e)
{
Debug.LogWarning("Loading Warning" + e.Message + e + e.InnerException);
}
finally
{
if (file != null)
{
file.Close();
}
}
}
public void SaveLevelData()
{
FileStream file = null;
try
{
BinaryFormatter bf = new BinaryFormatter();
file = File.Create(Application.dataPath + data_Path);
if (levelData != null)
{
levelData.Levels = levels;
bf.Serialize(file, levelData);
}
Debug.Log("Data saved");
}
catch (Exception e)
{
Debug.LogWarning("Saving Warning" + e.Message + e + e.InnerException);
}
finally
{
if (file != null)
{
file.Close();
}
}
}
One of the errors:
Loading WarningCould not find file "C:\Users\Lukas\AppData\LocalLow\LoopGameDev\Traffic Gameleveldata.dat"System.IO.FileNotFoundException: Could not find file "C:\Users\Lukas\AppData\LocalLow\LoopGameDev\Traffic Gameleveldata.dat"
File name: 'C:\Users\Lukas\AppData\LocalLow\LoopGameDev\Traffic Gameleveldata.dat'
at System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize, System.Boolean anonymous, System.IO.FileOptions options) [0x0019e] in <9577ac7a62ef43179789031239ba8798>:0
at System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share) [0x00000] in <9577ac7a62ef43179789031239ba8798>:0
at (wrapper remoting-invoke-with-check) System.IO.FileStream..ctor(string,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare)
at System.IO.File.Open (System.String path, System.IO.FileMode mode) [0x00000] in <9577ac7a62ef43179789031239ba8798>:0
Now it also tells me:
Saving WarningType 'UnityEditor.SceneAsset' in Assembly 'UnityEditor, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable.System.Runtime.Serialization.SerializationException: Type 'UnityEditor.SceneAsset' in Assembly 'UnityEditor, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable.
Does anybody know a fix? I really do not know why it's not working. I assume it's cause I'm not at runtime, but in Editor. I am using 2020.1.12f1 and this is a Collab project with 2 people working on it.
I highly appreciate any help! Stay safe and cheers!
Your answer
Follow this Question
Related Questions
Is EditorUtility.SetDirty restricted to prefabs or inspected GameObject? 5 Answers
Serialization Exception Error When Trying to Load 2 Answers
I am getting a serialization exception error when trying to save and load in Unity? 2 Answers
IOException: Sharing violation on path 1 Answer
How to structure my project so that I can save/load information? 0 Answers