Save player position usingJSON strings (675638)

Hi, I followed a video tutorial in which I’ve learnt how to save the player’s position; the problem is it worked for the first time, but now, when I stop the game, it does not save the last position any more.

Could you help me figure out what the problem is?

Edit: I found that my JSON string is empty. The only content is: “{}”
Here’s my script:

public class RecordLastPosition : MonoBehaviour {


    Transform MyTransform=null;
    Vector3 LastPos=Vector3.zero;
    Quaternion LastRot = Quaternion.identity;


    //SavePosition before going to Pause Menu
    void SaveData()
    {
        //Get the path in which Unity saves persistent data.  It consist of a Javascript Object Notation (JSON)
        string OutputPath = Application.persistentDataPath + @"ObjectPosition.json";  
        LastPos = MyTransform.position;
        LastRot = MyTransform.rotation;

        //Write the converted string into ObjectPosition file
        StreamWriter writer=new StreamWriter(OutputPath);
        writer.WriteLine(JsonUtility.ToJson(this));                        //Save THIS class data into text file
        writer.Close();
        Debug.Log("The output path is " + OutputPath);

    }

    void RetreiveData()
    {
        //Gets the strign containing the encoded data of position and rotation
        string InputPath = Application.persistentDataPath + @"ObjectPosition.json";
        StreamReader reader = new StreamReader (InputPath);
        string JSONString = reader.ReadToEnd ();
        Debug.Log ("The encoded string is " + JSONString);
        JsonUtility.FromJsonOverwrite (JSONString, this);                //Decode JSON and "pack" into THIS class
        reader.Close();

        MyTransform.position = LastPos;                                    //Set previously saved POS
        MyTransform.rotation = LastRot;                                    //Set previously saved ROT
    }
    // Use this for initialization
    void Awake () {
        MyTransform = GetComponent<Transform> ();
    }

    void Start()
    {
        RetreiveData ();
    }
  
    // It is called when the scene is cut off
    void OnDestroy () {
        SaveData ();
    }
}

None of your values are public, which generally means that they won’t get serialized. Try making them public or adding [System.Serializable] before the variables you wish to save.

Thank you, that was the problem