- Home /
Best way to load the position of my player?
So I have no trouble saving the position of my player to the text file and it comes out looking something like this:
-1256.434
24.50337
1644.17
But now I need to load that back into the game and spawn the player at that position, I know I need to convert each line into a float then assign it to x,y,z of the player but I'm not sure how to read it line by line.
function ReadFile(filepathIncludingFileName : String) {
sr = new File.OpenText(filePath);
input = "";
while (true) {
input = sr.ReadLine();
if (input == null)
{
break;
}
}
sr.Close();
}
Thats the code being used to read the text file but from there I'm not sure how to read the first line, assign it, read the second line, assign it then read and assign the third line.
Any help would be appreciated.
Cheers.
Answer by Tides · Aug 11, 2011 at 08:32 AM
Golly gosh I need to think more before I post these - I must look like an idiot.
To read and assign the position all I needed to do was this:
function ReadFile(filePath : String) {
sr = new File.OpenText(fullPath);
gameObject.Find("Player").transform.position.x = parseFloat(sr.ReadLine());
gameObject.Find("Player").transform.position.y = parseFloat(sr.ReadLine());
gameObject.Find("Player").transform.position.z = parseFloat(sr.ReadLine());
sr.Close();
}
That reads each line and converts the string to a float then puts the player in that position.