- Home /
My player figure falls down after load
I tried to find this error, but I was unsuccesfull. You can see on youtube video, that my character falls down after load. https://youtu.be/_CKNaYBxvhQ?t=1
here is save and load script
public void Save()
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create(Application.persistentDataPath + "/playerInfo.dat");
PlayerData data = new PlayerData();
data.x = transform.position.x;
data.y = transform.position.y;
data.z = transform.position.z;
bf.Serialize(file, data);
file.Close();
}
public void Load()
{
//Scene$$anonymous$$anager.LoadScene("enfos");
if (File.Exists(Application.persistentDataPath + "/playerInfo.dat"))
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + "/playerInfo.dat", File$$anonymous$$ode.Open);
PlayerData data = (PlayerData)bf.Deserialize(file);
file.Close();
Vector3 tempPos = transform.position;
//GameObject go = GameObject.Find("Player");
tempPos.x = data.x;
tempPos.y = data.y;
tempPos.z = data.z;
transform.position = new Vector3(data.x, data.y, data.z);
//transform.position = tempPos;
}
}
public delegate void SerializeAction();
public static event SerializeAction OnLoaded;
void OnEnable()
{
// Debug.Log("PrintOnEnable: script was enabled");
Load();
}
Answer by I_Am_Err00r · Jul 10, 2019 at 01:53 PM
To help you debug, try logging the Vector3 position on close so you can see what it says and then compare that with the load values, make sure they match.
Just looking at it though, what is this line of code doing when you have these:
Vector3 tempPos = transform.position;
//GameObject go = GameObject.Find("Player");
tempPos.x = data.x;
tempPos.y = data.y;
tempPos.z = data.z;
transform.position = new Vector3(data.x, data.y, data.z);
why do you have Vector3 tempos = transform.position, and then assing each x,y,z value based on it, then assign transform.position the values you just created based on it originally?
tempos is only old try to set coordinations can bě deleted works same
debug Save and Load are same coordinations after Load figure falls down from the box
public void Save()
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create(Application.persistentDataPath + "/playerInfo.dat");
PlayerData data = new PlayerData();
data.x = transform.position.x;
data.y = transform.position.y;
data.z = transform.position.z;
bf.Serialize(file, data);
file.Close();
}
public void Load()
{
//Scene$$anonymous$$anager.LoadScene("enfos");
if (File.Exists(Application.persistentDataPath + "/playerInfo.dat"))
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + "/playerInfo.dat", File$$anonymous$$ode.Open);
PlayerData data = (PlayerData)bf.Deserialize(file);
file.Close();
transform.position = new Vector3(data.x, data.y, data.z);
//transform.position = tempPos;
}
}
public delegate void SerializeAction();
public static event SerializeAction OnLoaded;
void OnEnable()
{
// Debug.Log("PrintOnEnable: script was enabled");
Load();
}
So you are saying that the coordinates are the same on save and load, how about logging during update to see if they move any from there? There wouldn't be any way that the positions would be exactly the same if on the save they are standing on top of something and the load they are not; the only reason that would occur is if somehow the level is moving slightly between saves and loads (but even then, the y coordinates at the least would be different from the drop in height).
Coordinates on Save and Load are same. After Load something happens what drops my figure down.
Your answer
Follow this Question
Related Questions
unity fontainebleau importing error 0 Answers
Legacy Animations and Unity 5 - What's The Deal? 1 Answer
Unity 5 fatal error 3 Answers
Unity 5 crash on startup 1 Answer
EXC_BAD_ACCESS (SIGABRT) from Unity::Material::CreateMaterial 0 Answers