- Home /
Serialization of an object in unity causing IO Exception Sharing Violation At Path
Hello all, I am having intense difficulty with my persistent score keeper. I am attempting to save a custom object of type ScoreList which contains only a list of type Score. I am then serializing this ScoreList.
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using UnityEngine;
public class FileCreator{
string fileName;
public ScoreList data;
FileStream file;
public FileCreator(string fileName)
{
this.fileName = Path.Combine(Application.persistentDataPath,fileName+".dat");
/*if(!File.Exists(this.fileName)) {
file = File.Create(this.fileName);
file.Close();
}*/
}
public void SerializeData()
{
file = File.Open(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite);
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(file, data);
file.Close();
file.Dispose();
}
public void DeserializeData()
{
file = File.Open(fileName, FileMode.OpenOrCreate, FileAccess.Read);
BinaryFormatter bf = new BinaryFormatter();
data = (ScoreList)bf.Deserialize(file);
file.Close();
file.Dispose();
}
}
I am getting an error at this line file = File.Open(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite);
The error is: IOException: Sharing violation on path C:\Users\Cameron\AppData\LocalLow\Wonderful3204\ToTheBeatVR\scoreinfo.dat
(Shortened)
Does anybody have any ideas? Can I not serialize a list? Both the list and the Score classes are listed as serializable.
Answer by Tourist · Oct 26, 2017 at 07:21 AM
Your file is opened somewhere else while you try to serialize it.
Your answer
Follow this Question
Related Questions
Making a scoreboard with a timer 2 Answers
Scoring System 3 Answers