- Home /
Help writing to file every update
I'm trying to save my players position every frame.
I'm having some trouble getting this to work... StreamWriter seems to only work when it is within a function, and I can't get it to, say, open a StreamWriter during Start() and then write lines during Update().
However, something is going wrong, here's the code:
import System;
import System.IO;
var Subject : int = 99;
function Start() {
var fileName = "NavData/sub" + Subject + "_nav.txt";
var sw : TextWriter = new StreamWriter(fileName,true);
if (File.Exists(fileName))
{
Debug.Log(fileName+" already exists.");
//EditorApplication.isPlaying = false;
}
// Write header
sw.WriteLine ("tX tY tZ rX rY rZ time");
sw.Flush();
}
function Update() {
// Get object information
var trans : Vector3 = transform.position;
var rot : Vector3 = Quaternion.identity.eulerAngles;
var ts = DateTime.Now;
print(ts);
//sw.WriteLine("test");
//sw.Close();
}
What you see above works, it writes a single line to file on start. However, if I uncomment
sw.WriteLine("test");
sw.Close();
I get the error: BCE0005: Unknown identifier: 'sw'.
I imagine this is because the StreamWriter is local to Start(), so when I move it outside of the Start() function, I instead get several errors.
Code:
import System;
import System.IO;
var Subject : int = 99;
var fileName = "NavData/sub" + Subject + "_nav.txt";
var sw : TextWriter = new StreamWriter(fileName,true);
function Start() {
if (File.Exists(fileName))
{
Debug.Log(fileName+" already exists.");
//EditorApplication.isPlaying = false;
}
// Write header
sw.WriteLine ("tX tY tZ rX rY rZ time");
sw.Flush();
}
function Update() {
// Get object information
var trans : Vector3 = transform.position;
var rot : Vector3 = Quaternion.identity.eulerAngles;
var ts = DateTime.Now;
print(ts);
sw.WriteLine("test");
sw.Close();
}
I get one error on start: IOException: Sharing violation on path /Users/Chris/Documents/Classwork/Epstein_Rotation/LocalGeometry/Maze_Unity/NavData/sub99_nav.txt
And another error every frame:
NullReferenceException: Object reference not set to an instance of an object writeTestC.Update () (at Assets/Scripts/writePosition.js:29)
In this latter case, the file is made, but nothing is written to it...
Any help is appreciated, thanks in advance!
Answer by OrbitSoft · Apr 07, 2015 at 07:40 AM
I think that you should keep the StreamWriter null and then assign it in Start function like this:
var sw : TextWriter = null;
function Start()
{
// the stream writer is assigned
sw = new StreamWriter(filename,true);
}
function Update()
{
sw.WriteLine("test");
sw.Close();
}
Answer by ethestel · Apr 07, 2015 at 07:56 AM
Hello,
Try instantiating StreamWriter object in your Start or Awake methods, and assign it to an instance variable.
import System;
import System.IO;
var Subject : int = 99;
var fileName = "NavData/sub" + Subject + "_nav.txt";
var sw : TextWriter;
function Start() {
sw = new StreamWriter(fileName,true);
if (File.Exists(fileName))
{
Debug.Log(fileName+" already exists.");
//EditorApplication.isPlaying = false;
}
// Write header
sw.WriteLine ("tX tY tZ rX rY rZ time");
sw.Flush();
}
function Update() {
// Get object information
var trans : Vector3 = transform.position;
var rot : Vector3 = Quaternion.identity.eulerAngles;
var ts = DateTime.Now;
print(ts);
sw.WriteLine("test");
sw.Close();
}
Answer by angelonc · Apr 07, 2015 at 02:12 PM
Thanks for your answers!
I had tried what you suggested (instantiating in awake), but it wasn't working, and that was due to the file being improperly closed from the previous time running the code.
Got it running by tightening up my exit procedure and following your suggestions:
import System;
import System.IO;
var sw : TextWriter = null;
function Start() {
var scene : String = Application.loadedLevelName;
var sceneparts = scene.Split("_"[0]);
var MazeNumber = int.Parse(sceneparts[1]);
var fileName = "NavData/sub" + placeObjects.SubjectNumber + "_" + MazeNumber + "_nav.txt";
sw = new StreamWriter(fileName);
//Write header
sw.WriteLine ("tX tY tZ rX rY rZ time");
sw.Flush();
}
function Update() {
// Get object information
var tx = transform.position.x;
var ty = transform.position.y;
var tz = transform.position.z;
var rx = transform.eulerAngles.x;
var ry = transform.eulerAngles.y;
var rz = transform.eulerAngles.z;
var epochStart = new System.DateTime(1970, 1, 1, 0, 0, 0, System.DateTimeKind.Utc);
var ts = (System.DateTime.UtcNow - epochStart).TotalSeconds;
sw.WriteLine(tx.ToString("F2") + "," + ty.ToString("F2") + "," + tz.ToString("F2") + "," + rx.ToString("F2") + "," + ry.ToString("F2") + "," + rz.ToString("F2") + "," + ts);
if (Input.GetKeyDown("escape")) {
print('Quitting...');
//EditorApplication.isPlaying = false;
sw.Close();
Application.LoadLevel("Menu");
}
}
Your answer
Follow this Question
Related Questions
Additional streamed scenes only for web player? 2 Answers
streaming to iPhone and Android 0 Answers
Video call in Unity? 1 Answer
Unity streaming audio. Rtsp question. 1 Answer
WWW class streaming high RAM usage 0 Answers