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!