Saving and Loading a GameObject's Position

Examples using Easy Save's API code
Post Reply
User avatar
Joel
Moodkie Staff
Posts: 3061
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 118 times
Joel @ Moodkie Interactive
Twitter - Unity

Post Reply