Writing position data to a csv file

I am creating a VR project where the position data of the headset and the virtual representation of a haptic stylus will need to be recorded to a csv file. I have a script set up that fetches this data and should write it to the file:

using UnityEngine;
using System;
using System.Collections.Generic;
using System.IO;

public class WritePositionData : MonoBehaviour
{
    private string positionDataFilePath;
    public GameObject Head;
    public GameObject Stylus;
    private List<string> positionData;

    void Start()
    {
        positionData = new List<string>();
        positionDataFilePath = Path.Combine(Application.persistentDataPath, "TEST.csv");

        // Write the header line to the position data file
        File.WriteAllText(positionDataFilePath, "TimeStamp,StylusY,StylusZ,StylusX,HeadY,HeadZ,HeadX\n");
    }

    void Update()
    {
        // Fetch the positions of the VR headset and stylus
        Vector3 currentHeadsetPosition = Head.transform.position;
        Vector3 currentStylusPosition = Stylus.transform.position;

        // Format the current positions into a string for the CSV file
        string positionDataLine = string.Format(
            "{0},{1},{2},{3},{4},{5},{6}",
            DateTime.Now,
            currentStylusPosition.x, currentStylusPosition.y, currentStylusPosition.z,
            currentHeadsetPosition.x, currentHeadsetPosition.y, currentHeadsetPosition.z
        );

        // Store the formatted string in the list
        positionData.Add(positionDataLine);
    }

    public void SavePositionData()
    {
        Debug.Log(positionDataFilePath);
        // Write the list of position data lines to the CSV file after each round
        File.AppendAllLines(positionDataFilePath, positionData);

        // Clear the list after saving
        positionData.Clear();
    }
}

However, when I view the csv file that is created, the proper headings are created, though no actual position data is found:

Is there something I am forgetting to reference within my code?

You’re never calling SavePositionData()

1 Like

Well, when do you call SavePositionData ? From a button or something? If not, yes, you don’t store any data in your file ^^.

So I amended that and called it in the Start function, but I’m still not having any data populate to the csv. I’m thinking maybe I need to call it in a different script?

That doesn’t make much sense. Start is always called before the first Update method is called. So of course when you save your current list of strings to the file in Start, that list would be empty. When do you actually want to save that list? Maybe when you exit the application? In that case you probably want to save the list in OnApplicationQuit.

Are you sure you understand your own logic here? ^^

2 Likes

I think I was looking for it to populate in real time rather after the application quits, though this worked. I appreciate the help.

Well, in that case you shouldn’t even use that list of strings and just write to the file as you go. However you shouldn’t use those static methods as they open the file, write to them and then close it again. This is very inefficient. You should open the file and keep the file / StreamWriter open and write to it as you go

public class WritePositionData : MonoBehaviour
{
    private string positionDataFilePath;
    public GameObject Head;
    public GameObject Stylus;
    private StreamWriter file;
    void Start()
    {
        positionData = new List<string>();
        positionDataFilePath = Path.Combine(Application.persistentDataPath, "TEST.csv");
        file = new StreamWriter(new FileStream(positionDataFilePath, FileMode.Create), Encoding.UTF8);
        // Write the header line to the position data file
        file.WriteLine("TimeStamp,StylusY,StylusZ,StylusX,HeadY,HeadZ,HeadX");
    }
    void Update()
    {
        // Fetch the positions of the VR headset and stylus
        Vector3 currentHeadsetPosition = Head.transform.position;
        Vector3 currentStylusPosition = Stylus.transform.position;
        // Format the current positions into a string for the CSV file
        string positionDataLine = string.Format(
            "{0},{1},{2},{3},{4},{5},{6}",
            DateTime.Now,
            currentStylusPosition.x, currentStylusPosition.y, currentStylusPosition.z,
            currentHeadsetPosition.x, currentHeadsetPosition.y, currentHeadsetPosition.z
        );
        file.WriteLine(positionDataLine);
    }
    void OnApplicationQuit()
    {
        file.Flush();
        file.Close();
    }
}

This would be more what you want to do.