Page 1 of 1

Cant save the position of a GameObject

Posted: Sat Apr 10, 2021 10:24 pm
by stucky8
Hey, Im trying to save the position of my Player Gameobject and I have created two simple save and load methods that execute on button press for that.


public GameObject player;
public void Save ()
{
ES3.Save<Vector3>("playerPosition", player.transform.localPosition);
}

public void Load()
{
player.transform.localPosition = ES3.Load<Vector3>("playerPosition");
}

But for some reason this is not working at all. Sometimes just nothing happens and sometimes I get an error saying the key playerPosition does not exist but the player never acctually changes its location.

Thanks in advance!

Re: Cant save the position of a GameObject

Posted: Sat Apr 10, 2021 10:42 pm
by Joel
Hi there,
Sometimes just nothing happens and sometimes I get an error saying the key playerPosition does not exist but the player never acctually changes its location.
This is because you're not checking whether save data exists before loading, or giving the ES3.Load method a default value to return if the data doesn't exist.

All the best,
Joel

Re: Cant save the position of a GameObject

Posted: Sun Apr 11, 2021 1:34 pm
by stucky8
Hey,

I have adjusted my code as you told me:

public GameObject player;
public Vector3 defaultValue;
public void Save()
{
ES3.Save("playerPosition", player.transform.localPosition);
}

public void Load()
{
player.transform.localPosition = ES3.Load("playerPosition", defaultValue);
}

And while I dont get any errors now, the players Position still does not change when I load.
Thanks for helping!

Re: Cant save the position of a GameObject

Posted: Sun Apr 11, 2021 1:40 pm
by Joel
Hi there,

Please could you replicate this in a new project with a basic scene and private message it to me with instructions?

All the best,
Joel