- Home /
Writing Vector3 on a text file
Hi everybody,
I'm trying to save (for each fixedupdate) the position of my character and the game time. I had a look at the I/O documentations but apparently I am still using the wrong class cause I am not able to write Vector3 in a txt file. This is the error: No appropriate version of 'System.IO.TextWriter.WriteLine' for the argument list '(float, UnityEngine.Vector3)' was found. This is the code:
import System;
import System.IO;
var fileName = "gameobject.txt";
function FixedUpdate () {
if (File.Exists(fileName)) {
Debug.Log(fileName +" already exists.");
return;
}
var sr = File.CreateText(fileName);
sr.WriteLine ("Position file for each fixedupdate.") ;
sr.WriteLine ( Time.fixedTime, gameObject.transform.position);
sr.Close ();
}
What am I missing?? Thanks so much
Answer by Eric5h5 · Oct 23, 2011 at 10:03 PM
You really don't want to create and close a file every FixedUpdate. Anyway, you can see the docs for WriteLine here. As you can see, there isn't any method that takes a float and a Vector3 (which doesn't exist in .NET anyway). So you have to convert to something that WriteLine can use, such as splitting it into 3 separate floats, or using ToString.
Answer by inquisitive_me90 · Oct 24, 2011 at 05:20 PM
yea, totally agree with Eric5h5 about the fixedupdate comment and his suggestions..
you could simply deal with the float and vector3 problems as below :
var t : flat = Time.fixedTime;
var position : Vector3;
// initialise 'position' to the value required !
var x : float = position[0];
var y : float = position[1];
var z : float = position[2];
sr.WriteLine ("Time and Vector3 coordinates are: {0},{1},{2} and {3}", t, x, y, z);