Saving and Loading a GameObject's Position

Examples using Easy Save's API code
Post Reply
User avatar
Joel
Moodkie Staff
Posts: 3676
Joined: Wed Nov 07, 2012 10:32 pm

Saving and Loading a GameObject's Position

Post 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);
    }
}
Attachments
SavePosition.cs
(383 Bytes) Downloaded 234 times
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started

hrohibil
Posts: 10
Joined: Thu Aug 19, 2021 5:46 am

Re: Saving and Loading a GameObject's Position

Post by hrohibil »

Please help, I want to use the save and load on buttons.

How do i set this up?

User avatar
Joel
Moodkie Staff
Posts: 3676
Joined: Wed Nov 07, 2012 10:32 pm

Re: Saving and Loading a GameObject's Position

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

hrohibil
Posts: 10
Joined: Thu Aug 19, 2021 5:46 am

Re: Saving and Loading a GameObject's Position

Post 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

User avatar
Joel
Moodkie Staff
Posts: 3676
Joined: Wed Nov 07, 2012 10:32 pm

Re: Saving and Loading a GameObject's Position

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

hrohibil
Posts: 10
Joined: Thu Aug 19, 2021 5:46 am

Re: Saving and Loading a GameObject's Position

Post by hrohibil »

Thanks Joel

It works perfectly now...

Br Hamid

Post Reply