- Home /
Change Players Position On Scene Start
So I have a Save and Load method and it all works except it's not all working properly. I have health being saved which loads and saves fine and also X and Z positions. Later I have another script using the Awake method I set the player's position to a new Vector3 using the X and Y positions that are saved but the player's position doesn't move. Is there a fix for this or is there a better way to do this entirely.
Save/Load Methods public void Save(){
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create(Application.persistentDataPath + "/GameData.dat");
//Data to write
PlayerData data = new PlayerData();
data.health = health;
data.posX = posX;
data.posZ = posZ;
bf.Serialize(file, data);
file.Close();
Debug.Log("Saved");
}
public void Load(){
if(File.Exists(Application.persistentDataPath + "/GameData.dat")){
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + "/GameData.dat", FileMode.Open);
PlayerData data = (PlayerData)bf.Deserialize(file);
file.Close();
//Data to Load
health = data.health;
posX = data.posX;
posZ = data.posZ;
Debug.Log("Loaded");
}
Setting the player's position
void Start(){
Player.transform.position = new Vector3(manager.posX, 0.5f, manager.posZ);
}
void Update(){
Debug.Log("Pos X: " + manager.posX);
Debug.Log("Pos Z: " + manager.posZ);
}
Comment
Your answer
Follow this Question
Related Questions
Saving with PlayerPrefs? How it works? 1 Answer
Camera rotation around player while following. 6 Answers
Saving System? 1 Answer
saving the game in position 1 Answer