- Home /
Cannot read or edit .txt file
Hi, I'm new to unity and this probably was asked multiple times, but I cannot seem to find the solution to my problem. I'm making a simple HiLow card game that use Score.txt file, which I placed in Assets folder, to store last score and high score. The problem occurs when I build the project. Bear in mind that everything works perfectly in unity editor. For some reason it doesn't go after scoreSave() function is called and doesn't load a new scene. So basiclly you never lose the game. I tried removing it and every thing worked perfectly. So I assume this is the problem. This is the part of the code:
public void scoreSave()
{
StreamReader sr = new StreamReader(path);
string lastScore = sr.ReadLine();
string highScore = sr.ReadLine();
sr.Close();
int hScore = Convert.ToInt32(highScore);
if(hScore < score)
{
hScore = score;
}
StreamWriter sw = new StreamWriter(path);
sw.WriteLine(score);
sw.WriteLine(hScore);
sw.Close();
}
I tried to find a solution but no luck. I found some articles that says to check "output_log.txt", but it doesn't exist neither in C:\Users\username\AppData\Local\CompanyName\ProductName\output_log.txt neither in game directory. I did a scan on C: and D: discs but no luck.
Any suggestions?
Answer by DomantasMot · May 10, 2020 at 11:44 AM
Okey. I managed to solve it and end my suffering. In the same scoreSave() function that is shown above at the begining of it I put this code and it fixed everything:
string path = Application.dataPath + "/Score.txt";
if (!File.Exists(path))
{
File.WriteAllText(path, "0\n0");
}
Now the .txt file is in game directory projectName_Data folder.
Your answer
Follow this Question
Related Questions
Is there a way to retrieve a previously saved scene? 2 Answers
Save sprite in custom folder 1 Answer
Saved files deleted after creating a new build 0 Answers
How do I save a List in my project? 0 Answers
Save scene 1 Answer