Adding to a text file. It works but I get this message afterwoods
Hi guys/girls! I'm trying to write to a text file and I keep getting this error message.
IOException: Sharing violation on path D:\githubStuff\Assets\HighScores.txt
System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean anonymous, FileOptions options) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.IO/FileStream.cs:320)
System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share)
(wrapper remoting-invoke-with-check) System.IO.FileStream:.ctor (string,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare)
System.IO.StreamWriter..ctor (System.String path, Boolean append, System.Text.Encoding encoding, Int32 bufferSize) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.IO/StreamWriter.cs:124)
System.IO.StreamWriter..ctor (System.String path)
(wrapper remoting-invoke-with-check) System.IO.StreamWriter:.ctor (string)
GameManager.Awake () (at Assets/Scripts/GameManager.cs:50)
It does write to the file but I'm not sure why this message pops up.
Here is my Code
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;
public class GameManager : MonoBehaviour {
public static GameManager instance = null;
private TowerManager tower;
private DropperCamera camera;
private CameraFollow playerCamera;
private GameObject player;
private GameObject blobbi;
private GameObject shadow;
private StreamWriter sw;
// Are we using a controller or KB/mouse?
public bool controller = false;
void OnLevelWasLoaded(int level) {
tower = GameObject.Find("Tower").GetComponent<TowerManager>();
// TODO: DON'T HARDCODE YOUR NAMES NICK YOU MORON
camera = GameObject.Find("IsoCamera").GetComponent<DropperCamera>();
playerCamera = GameObject.Find("Main Camera").GetComponent<CameraFollow>();
player = GameObject.Find("Player");
blobbi = GameObject.Find("Blobbi");
shadow = GameObject.Find("ShadowGoo");
// Ignore physical collisions between the blocks and the world
// We're doing this manually instead
Physics.IgnoreLayerCollision(0, 8);
// Also blocks and other blocks
Physics.IgnoreLayerCollision(8, 8);
}
// Use this for initialization
void Awake () {
OnLevelWasLoaded (1);
// Singleton stuff - there can only be one
if (instance == null)
{
instance = this;
} else if (instance != this) {
Destroy(instance);
}
DontDestroyOnLoad(instance);
sw = new StreamWriter ("Assets/HighScores.txt");
}
public GameObject getPlayer()
{
return player;
}
void RestartLevel()
{
string line = (getPlayer ().transform.position.y / 2).ToString ();
SaveHighScore (line);
Application.LoadLevel (Application.loadedLevelName);
}
// Update is called once per frame
void Update () {
if (Input.GetKey (KeyCode.Return))
{
RestartLevel();
}
}
void SaveHighScore(string line){
sw.WriteLine (line);
sw.Flush();
}
Answer by Suddoha · Sep 28, 2015 at 12:28 AM
You never properly close/dispose the stream writer, so another script might try to access the file and that usually results in conflicts. I recommend to only open the file when you really need it and dispose it afterwards. There's a nice way to do that (you should do that in the Save method):
using(StreamWriter sw = new StreamWriter("YourFile"))
{
// do the stuff
}
Using this syntax you won't need to worry about properly disposing. There are still cases that you might need to keep the Stream object for a while in a variable, then you have to fall back to manually Closing/Disposing.
Also, return in the else-part of your Awake method as everything else afterwards will still be executed (Destroy does not destroy immediately, but eventually during the frame, that can be at the very end).
Just added the missing closing parenthesis. Sorry for that.
Your answer
Follow this Question
Related Questions
Writing to the Externally Mounted SD Card Android 1 Answer
Why is my code not working? [please help] 2 Answers
Reading from file for leaderboard 0 Answers
Help Writing and reading files on android. 1 Answer
Writing Files to Android Device 0 Answers