I make a code to write a text file named “SaveEssentials” on a folder named “Save”, the problem is, when i run the game and run the script, the folder and the file are created, but there is nothing in the file.
This error shows me up every time when i run the game:
DirectoryNotFoundException: Could not find a part of the path “C:\Users\User\Documents\Unity\RPG\Save\SaveEssentials.txt”.
System.IO.FileStream…ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean anonymous, FileOptions options) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.IO/FileStream.cs:292)
System.IO.FileStream…ctor (System.String path, FileMode mode, FileAccess access, FileShare share)
(wrapper remoting-invoke-with-check) System.IO.FileStream:.ctor (string,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare)
System.IO.StreamWriter…ctor (System.String path, Boolean append, System.Text.Encoding encoding, Int32 bufferSize) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.IO/StreamWriter.cs:124)
System.IO.StreamWriter…ctor (System.String path, Boolean append)
(wrapper remoting-invoke-with-check) System.IO.StreamWriter:.ctor (string,bool)
System.IO.File.CreateText (System.String path) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.IO/File.cs:159)
SaveGame…ctor () (at Assets/Scripts/Objects/Game/SaveGame.js:8)
And here is my code:
#pragma strict
import System;
import System.IO;
var folder : String = "Save";
var fileName : String = "SaveEssentials";
var extension : String = "txt";
var crFile = File.CreateText("./" + folder + "/" + fileName + "." + extension);
var triggered : boolean = false;
function OnTriggerEnter2D(EventCollider: Collider2D) {
triggered = true;
}
function OnTriggerExit2D(EventCollider: Collider2D) {
triggered = false;
}
function Update () {
if (triggered == true){
if (Input.GetKeyDown ("return")){
if (Directory.Exists("./" + folder)){
crFile.WriteLine (GameEssentials.playerName);
crFile.WriteLine (GameEssentials.level);
crFile.WriteLine (GameEssentials.exp);
}
else
{
Directory.CreateDirectory ("./" + folder);
crFile.WriteLine (GameEssentials.playerName);
crFile.WriteLine (GameEssentials.level);
crFile.WriteLine (GameEssentials.exp);
}
}
}
}
GameEssentials is another script