0
\$\begingroup\$

State class :

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

[Serializable]
public class SaveState
{
    public float posX;
    public float posY;
    public float posZ;

    public float rotX;
    public float rotY;
    public float rotZ;
    public float rotW;

    public float scaX;
    public float scaY;
    public float scaZ;

    public DateTime LastSaveTime { set; get; }
}

Manager script :

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

public class SaveManager : MonoBehaviour
{
    public static void Save(SaveState player)
    {
        BinaryFormatter formatter = new BinaryFormatter();
        string path = Application.persistentDataPath + "/player.bin";
        FileStream stream = new FileStream(path, FileMode.Create);

        SaveState data = new SaveState();

        formatter.Serialize(stream, data);
        stream.Close();
    }

    public static SaveState Load()
    {
        string path = Application.persistentDataPath + "/player.bin";
        if (File.Exists(path))
        {
            BinaryFormatter formatter = new BinaryFormatter();
            FileStream stream = new FileStream(path, FileMode.Open);

            SaveState data = formatter.Deserialize(stream) as SaveState;
            stream.Close();

            return data;
        }
        else
        {
            Debug.LogError("Save file not found in " + path);
            return null;
        }
    }
}

Test script :

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

public class SaveTest : MonoBehaviour
{
    private void Update()
    {
        if(Input.GetKeyDown(KeyCode.T))
        {
            var player = GameObject.Find("Player");
            SaveState saveState = new SaveState();
            saveState.posX = player.transform.position.x;
            saveState.posY = player.transform.position.y;
            saveState.posZ = player.transform.position.z;

            saveState.rotX = player.transform.rotation.x;
            saveState.rotY = player.transform.rotation.y;
            saveState.rotZ = player.transform.rotation.z;
            saveState.rotW = player.transform.rotation.w;

            saveState.scaX = player.transform.localScale.x;
            saveState.scaY = player.transform.localScale.y;
            saveState.scaZ = player.transform.localScale.z;

            //Save data from PlayerInfo to a file named players
            SaveManager.Save(saveState);
        }

        if(Input.GetKeyDown(KeyCode.L))
        {
            //Display loaded Data
            var player = GameObject.Find("Player");
            player.transform.position = new Vector3(SaveManager.Load().posX, SaveManager.Load().posY, SaveManager.Load().posZ);
            player.transform.rotation = new Quaternion(SaveManager.Load().rotX, SaveManager.Load().rotY, SaveManager.Load().rotZ, SaveManager.Load().rotW);
            player.transform.localScale = new Vector3(SaveManager.Load().scaX, SaveManager.Load().scaY, SaveManager.Load().scaZ);
        }
    }
}

I can see the created saved file player.bin 1 KB size.

This is a screenshot when I press the T button to save : On the left bottom game window. And the inspector player info position and rotation when saving.

I press the T button once to save while the player is in coroutine. When the game start the player is not moving but rotating using coroutine. Not sure if the coroutine if saving while the player is rotating make the problem.

When saving

This is a screenshot when pressing the L button and the inspector of the player position and rotation :

When loading

Not same position and rotation when loading.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ This looks very similar to your previous, now-deleted question. In future, please edit your question rather than deleting and re-creating it. \$\endgroup\$ – DMGregory Dec 13 '19 at 14:35
1
\$\begingroup\$
public static void Save(SaveState player) {
    BinaryFormatter formatter = new BinaryFormatter();
    string path = Application.persistentDataPath + "/player.bin"; 
    FileStream stream = new FileStream(path, FileMode.Create);
    SaveState data = new SaveState();
    formatter.Serialize(stream, data);
    stream.Close();
}

This code ignores the save data passed in as the argument player. Instead it creates a new, default/empty save called data, and saves this to your file. So naturally when you read it, you get default values.

Unrelated, your load function is repeatedly opening the file, reading its contents, closing the file, then using just one of the values it read and throwing out the rest. Then it opens the file again, reads all its contents, uses the next value, and throws out the rest again...

Just read the file once into a variable, and re-use that variable to populate the various parameters you need.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.