Page 1 of 1

Saving and Loading a GameObject's Position

Posted: Sat Apr 18, 2020 1:55 pm
by Joel
Saving and Loading a GameObject's position

The following script can be attached to a GameObject to save it's position when it's Destroyed (this happens when the scene is changed, and when the application quits), and load it's position when the scene loads.

Code: Select all

using UnityEngine;

public class SavePosition : MonoBehaviour
{
    // Generate a unique ID for this GameObject.
    public string guid = System.Guid.NewGuid().ToString();

    void Awake()
    {
        transform.localPosition = ES3.Load<Vector3>(guid, transform.localPosition);
    }

    void OnDestroy()
    {
        ES3.Save<Vector3>(guid, transform.localPosition);
    }
}

Re: Saving and Loading a GameObject's Position

Posted: Thu Aug 19, 2021 5:52 am
by hrohibil
Please help, I want to use the save and load on buttons.

How do i set this up?

Re: Saving and Loading a GameObject's Position

Posted: Thu Aug 19, 2021 7:32 am
by Joel
Hi there,

Buttons are part of Unity's functionality, so you can find information on this in their docs:
https://learn.unity.com/tutorial/creating-ui-buttons

All the best,
Joel

Re: Saving and Loading a GameObject's Position

Posted: Thu Aug 19, 2021 7:36 am
by hrohibil
Thank you for reply.

I know how to get the buttons work, but i could not directly transfer the code from the :

Code: Select all

public class SavePosition : MonoBehaviour
{
    // Generate a unique ID for this GameObject.
    public string guid = System.Guid.NewGuid().ToString();

    void Awake()
    {
        transform.localPosition = ES3.Load<Vector3>(guid, transform.localPosition);
    }

    void OnDestroy()
    {
        ES3.Save<Vector3>(guid, transform.localPosition);
    }
}
So what code would i enter to have my players position?
Thank you

Re: Saving and Loading a GameObject's Position

Posted: Thu Aug 19, 2021 7:39 am
by Joel
Hi there,

You would simply hook up the Save button to the OnDestroy method, and the Load button to the Awake method.

As the questions you are asking are general Unity programming questions rather than ones specific to Easy Save, I recommend you ask for more advice on the Unity forums. Unfortunately I'm only able to assist with things directly relating to Easy Save.

All the best,
Joel

Re: Saving and Loading a GameObject's Position

Posted: Thu Aug 19, 2021 8:13 am
by hrohibil
Thanks Joel

It works perfectly now...

Br Hamid

Re: Saving and Loading a GameObject's Position

Posted: Sat May 27, 2023 1:07 pm
by student80
If I use this to save a unit's HP, Exp , etc. Will a bad actor be able to open the app and edit the save file directly ? I saw the encryption function but don't really want to mess up app store submission guidelines...

Re: Saving and Loading a GameObject's Position

Posted: Sun May 28, 2023 10:22 am
by Joel
Hi there,

No file stored on a user's device is entirely secure. Even the Unity application itself can be tampered with. I recommend taking a look at the encryption guide for more information:

https://docs.moodkie.com/easy-save-3/es ... mpression/

This tends to be less of an issue on iOS as the user would need to jailbreak their device in order to access the file. However, if security is of utmost importance to you then you would need to consult a security analyst as you would likely need a solution which is bespoke to your project.

All the best,
Joel