Cant save the position of a GameObject

Discussion and help for Easy Save 3
Post Reply
stucky8
Posts: 2
Joined: Sat Apr 10, 2021 10:05 pm

Cant save the position of a GameObject

Post 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!
User avatar
Joel
Moodkie Staff
Posts: 4826
Joined: Wed Nov 07, 2012 10:32 pm

Re: Cant save the position of a GameObject

Post 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
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
stucky8
Posts: 2
Joined: Sat Apr 10, 2021 10:05 pm

Re: Cant save the position of a GameObject

Post 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!
Last edited by stucky8 on Sun Apr 11, 2021 1:43 pm, edited 1 time in total.
User avatar
Joel
Moodkie Staff
Posts: 4826
Joined: Wed Nov 07, 2012 10:32 pm

Re: Cant save the position of a GameObject

Post 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
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Post Reply