So i use script like this to save player position from previous scene
void Writefile() {
X = target.transform.position.x;
Y = target.transform.position.y;
string path = "Assets/PlayerPos.txt";
StreamWriter save = new StreamWriter (path);
save.WriteLine (X);
save.WriteLine (Y);
save.Close ();
}
and then i attach load script to empty object in another scene to read & load the saved position in file
void readFile() {
string path = "Assets/PlayerPos.txt";
StreamReader read = new StreamReader (path);
X = read.ReadLine();
Y = read.ReadLine();
posX = float.Parse(X);
posY = float.Parse(Y);
Debug.Log (posX);
Debug.Log (posY);
}
void ChangePos(){
target.transform.position = new Vector2 (posX, posY);
}
}
Even though that the position from previous scene already written in the file but when i moved to another and want to load the saved position the player position didnt change to the same position from previous scene. is there something wrong with my code? i’m new to C# so sorry if my code look like a mess or inefficient