Good Morning to everybody. I’m using StreamReader and StreamWriter in a c# script to read/write on a txt file (the file is situated in the same folder where I’ll create the project build, therefore outside Project_data). Before the build, StreamReader and StreamWriter work both correctly and read/write on the file. I’ve tried to debug the script on Unity. Every instruction is executed correctly. When I create the build project, it works only StreamWriter or StreamReader. I’ve tried to use File.WriteAllText and File.ReadAllText, but I found the same problem. I have no idea how to fix the issue.
I have no error message returned because the code is correctly executed in Unity.
Only StreamReader works when I create a build project.
This is the code that I use to read/write on the file:
using UnityEngine;
using System.Collections;
using System.Diagnostics;
using System.IO;
using System;
using System.Text;
public class Countdown : MonoBehaviour {
//Create a timer of 180s
float timeRemaining = 180;
//Get informations from the current process
Process currentProcess = Process.GetCurrentProcess();
//Number of objects to show
int n_cues = 0;
//Arguments imported
string arguments_string;
//Array to read/write arguments
char[] n_cues_argument_char = new char[2];
StreamReader sr;
StreamWriter sw;
// Use this for initialization
void Start() {
/* Reading of the file*/
try
{
sr = new StreamReader("C:\\Users\\LaptopLab\\Desktop\\Scene\\arguments.txt");
arguments_string = Convert.ToString(sr.ReadLine());
sr.Close();
arguments_string.CopyTo(0, n_cues_argument_char, 0, arguments_string.Length);
n_cues = Convert.ToInt32(n_cues_argument_char[1].ToString());
}
catch (Exception e)
{
Console.WriteLine("Exception: " + e.Message);
}
}
// Update is called once per frame
void Update() {
timeRemaining -= Time.deltaTime;
}
void OnGUI()
{
//While timer is not zero, view current timer value on UI
if (timeRemaining > 0)
{
GUI.Label(new Rect(100, 100, 200, 100), "Time Remaining : " + (int)timeRemaining);
GUI.Label(new Rect(100, 120, 200, 100), "N_Cues: " + n_cues);
GUI.Label(new Rect(100, 160, 200, 100), "Arguments_Readed : " + arguments_string);
}
else
{
//Update n_cues
if (n_cues == 5)
n_cues = 1;
else
n_cues++;
//Update array of characters with the arguments modified
n_cues_argument_char[0] = n_cues_argument_char[0];
n_cues_argument_char[1] = Convert.ToChar(n_cues.ToString());
/* Write a word on the file */
try
{
sw = new StreamWriter("C:\\Users\\LaptopLab\\Desktop\\Scene\\arguments.txt");
arguments_string = new string(n_cues_argument_char);
sw.WriteLine(n_cues_argument_char);
sw.Close();
}
catch (Exception e)
{
Console.WriteLine("exception: " + e.Message);
}
finally
{
Console.WriteLine("executing finally block 2");
}
//Close the current process
currentProcess.Kill();
}
}
}
Thanks for your help!