0

The save state class :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

[Serializable]
public class SaveState
{
    public float RotationX { set; get; }
    public float RotationY { set; get; }
    public float RotationZ { set; get; }

    public DateTime LastSaveTime { set; get; }
}

The save manager :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Runtime.Serialization.Formatters.Binary;
using System;
using System.IO;

public class SaveManager : MonoBehaviour
{
    #region Instance
    private static SaveManager instance;
    public static SaveManager Instance
    {
        get
        {
            if (instance == null)
            {
                instance = FindObjectOfType<SaveManager>();
                if (instance == null)
                {
                    instance = new GameObject("Spawned SaveManager", typeof(SaveManager)).GetComponent<SaveManager>();
                }
            }

            return instance;
        }
        set
        {
            instance = value;
        }
    }
    #endregion

    [Header("Logic")]
    [SerializeField] private string saveFileName = "data.dat";
    [SerializeField] private bool loadOnStart = true;
    private BinaryFormatter formatter;

    private SaveState state;
    public SaveState State { get => state; set => state = value; }

    private void Start()
    {
        // Init the formatter.
        formatter = new BinaryFormatter();
        DontDestroyOnLoad(this.gameObject);

        // If loadOnStart is true try to load the saved file.
        if (loadOnStart == true)
        {
            Load();
        }
    }

    public void Save()
    {
        // If no state
        if (state == null)
        {
            state = new SaveState();
        }

        //Set the time when trying to save.
        state.LastSaveTime = DateTime.Now;

        //Open the file.
        var file = new FileStream(saveFileName, FileMode.OpenOrCreate, FileAccess.Write);
        formatter.Serialize(file, state);
        file.Close();
    }

    public void Load()
    {
        // Open file
        try
        {
            var file = new FileStream(saveFileName, FileMode.Open, FileAccess.Read);

            // Read file.
            state = (SaveState)formatter.Deserialize(file);
            file.Close();
        }
        catch
        {
            Debug.Log("No save file found, creating a new entry");
            Save();
        }
    }
}

And a test script :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SaveTest : MonoBehaviour
{
    private void Update()
    {
        SaveManager saveManager = SaveManager.Instance;

        if(Input.GetKeyDown(KeyCode.T))
        {
            var player = GameObject.Find("Player");
            saveManager.State.RotationX = player.transform.position.x;
            saveManager.State.RotationY = player.transform.position.y;
            saveManager.State.RotationZ = player.transform.position.z;

            saveManager.Save();
        }

        if(Input.GetKeyDown(KeyCode.L))
        {
            SaveManager.Instance.Load();
        }
    }
}

When I press the T key I used a break point it's getting there finding the player. But when pressing the L key it does nothing it's not changing the player rotation to what have been saved.

I can see the file is created data.dat 256 bytes size.

Remy
  • 4,843
  • 5
  • 30
  • 60
Daniel Lip
  • 3,867
  • 7
  • 58
  • 120
  • 4
    Your `Load` method isn't trying to set any values on the `player` – UnholySheep Dec 12 '19 at 22:49
  • 1
    Does this answer your question? [What is the best way to save game state?](https://stackoverflow.com/questions/40965645/what-is-the-best-way-to-save-game-state) – Ruzihm Dec 12 '19 at 22:56

1 Answers1

2

You are only loading in the data from your save file, but not actually doing anything with the data. If you want to set the player's rotation based on whatever data is inside your downloaded state you need to make a call to that data.

public void Load()
{
    // Open file
    try
    {
       var file = new FileStream(saveFileName, FileMode.Open, FileAccess.Read);

       // Read file.
       state = (SaveState)formatter.Deserialize(file);
       file.Close();

       //After obtaining the SaveState we apply the values to our player's local rotation
       player.transform.localRotation = Quaternion.Euler(state.RotationX, state.RotationY, state.RotationZ);
    }
    catch
    {
       Debug.Log("No save file found, creating a new entry");
       Save();
    }
}

(Note that although i'm assigning the values to the player inside your Load method here, I would recommend doing that in a separate method for cleaner code)

Remy
  • 4,843
  • 5
  • 30
  • 60