Reputation: 41
I am making a game in unity, it is almost completed but I am having a issue with saving and loading player's position. I am using custom binary files method. Whenever I hit save button, and then load the game, it shows the saving position for a second and then again show me on my loading position. I want my player to remain on the saving position. I used the Brackeys tutorial to do this.
public void SaveGame()
{
PlayerPositionData save = new PlayerPositionData();
save.position[0] = transform.position.x;
save.position[1] = transform.position.y;
save.position[2] = transform.position.z;
SavingSystem<PlayerPositionData>
.Save(Application.persistentDataPath + "/file10.pqr", save);
}
public void LoadGame()
{
PlayerPositionData data = SavingSystem<PlayerPositionData>
.Load(Application.persistentDataPath + "/file10.pqr");
Vector3 position;
position.x = data.position[0];
position.y = data.position[1];
position.z = data.position[2];
transform.position = position;
}
[System.Serializable]
public class PlayerPositionData
{
public float[] position;
public PlayerPositionData()
{
position = new float[3];
}
}
The saving and loading functions are in PlayerPosition class which is a component of my player. I am not getting any error in console. not a single one. Also, at various points, I print my position using debug.log, so I am sure that my position is correctly saving, loading, and assigning into the player's position. But after a second, it returns me to the position at which I load the game. Other things correctly saving and loading like number of bullets, health etc but there is a problem in player's position.
Also, I made a seperate loading and saving functions where binary formatters and stream work is going on. Can anyone help me? I am a student and I have to submit this project in this week. I am working on this position problem since one week but I got no clue how to fix this.
Upvotes: 2
Views: 834
Reputation: 11
I was facing similar issue, resolved it by following the instructions by @khubiab. Much appreciated.
Upvotes: 0
Reputation: 41
I found the answer to my question. You should have checked the tick on "Auto Sync Transform" which is on Edit >> Project Settings >> Physics. It will allows you to set the position of a gameobject through coding.
Upvotes: 2