Hello, i’m sorry to ask things about streamwriter here because it’s more a C# problem but as I use it with unity maybe there’s someone to save me.
My aim is to store data from an eye tracker into a txt file so I try do it with this script :
using System.Text;
using System;
using System.Collections.Generic;
using UnityEngine.UI;
using System.IO;
using System.Linq;
using Newtonsoft.Json;
using TETCSharpClient;
using TETCSharpClient.Data;
public class Enregistrements : MonoBehaviour{
// public string folder = "Enregistrements";
string saveFilePath = "gazedata.txt";
StreamWriter logWriter;
public void onGazeUpdate (GazeData gazeData)
{
if (logWriter == null) {
logWriter = new StreamWriter (File.Open (saveFilePath, FileMode.Create, FileAccess.Write, FileShare.Read));
}
logWriter.WriteLine (gazeData.SmoothedCoordinates.X + ":" + gazeData.SmoothedCoordinates.Y);
logWriter.Flush ();
}
}
But there’s no txt file created so it’s like the scirpt do not work even if I do not have any errors.
I used to go to the msdn site for streamwriter so I copy this following script from the site :
using UnityEngine;
using System.Collections;
using System.Text;
using System;
using TETCSharpClient;
using TETCSharpClient.Data;
using UnityEngine.UI;
using System.IO;
using System.Linq;
public class test : MonoBehaviour {
static void Main()
{
string[] lines = { "First line", "Second line", "Third line" };
using (System.IO.StreamWriter file =
new System.IO.StreamWriter (@"C:\Users\BROUSSAM\Documents\Planetes\Enregistrements\WriteLines2.txt")) {
foreach (string line in lines) {
if (!line.Contains ("Second")) {
file.WriteLine (line);
}
}
}
}
}
But I’ve got the same problem, I do not have any txt files created.
Your original script (the one with onGazeUpdate) shouldn’t compile - you’re defining saveFilePath locally in Start, and then using it in onGazeUpdate. You’ll need to post the actual script that’s failing.
The one copied from MSDN should work as long as the path you give is valid. If it’s inside of Unity, you’ll have to call the Main method manually - the example assumes that it’s the entry point of the app, which isn’t the case if you’re running on top of Unity.
Thanks so if I understand I have to change the fact that my saveFilePath is define in start ? Because I have no error expect that it does not recording. Do I have to put the saveFilePath directly in onGazeUpdate ?
Feel sorry to ask but i’m begginer and I’ve got some issues with the syntaxe
This is why indentation is important, though - your fields are tabbed to the right so they look like they’re part of a method, and I was a bit quick on the post trigger :p. Your first script works just fine, you just have to find the text file you created.
Okay, so in Unity, if you write to a file named “gazedata.txt”, without giving a path, the file will be placed on the root of your project folder - next to your Assets folder. Check there. I believe that in a build, the file will be placed in the data folder, but I’m not sure.
Exactly why it’s placed there, I’m not sure - I’m assuming the Unity runtime sets some flags for what should be considered the “root” everything should be placed in.
The “correct” place to put data from your game is in Application.PersistentDataPath, which points to (on Windows) Appdata/YourCompany/YourGame (company and game is set up in the player settings). So your first script should probably point to
Ok thanks , I’ve search in all my computer and there’s no life sign of my file.
Even if I put a PersistentDataPath the same problem occurs…
I’ve trie to put a Debug to see if the script is read but it seems that it isn’t, my debut don’t appear. I’ve attach my script to my Main Camera but do I have to call back my function in another script ?
Yeah, the function won’t just automagically call itself, you have to actually invoke it.
Where are you supposed to be getting the onGazeUpdate from? Sounds like some third party plugin. You should check their documentation and tutorials to see how that’s supposed to work.
All that is in onGazeUpdate came from a plugin. So I’ve got differents scripts to conect the eye tracker to unity where onGazeUpdate is in so maybe I do not have the right to make a public void with the same name no ?
Guess I could find informations on their documentations