unity – What is wrong with my save/load system?


The SaveSystem class :

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

public static class SaveSystem
{
    public static void SavePlayer(Player player)
    {
        BinaryFormatter formatter = new BinaryFormatter();

        string path = Application.persistentDataPath + "/player.bin";
        FileStream stream = new FileStream(path, FileMode.Create);

        PlayerData data = new PlayerData(player);

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

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

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

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

            return null;
        }
    }
}

The Player class :

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

public class Player : MonoBehaviour
{
    public void SavePlayer()
    {
        SaveSystem.SavePlayer(this);
    }

    public void LoadPlayer()
    {
        PlayerData data = SaveSystem.LoadPlayer();

        Vector3 position;
        position.x = data.position(0);
        position.y = data.position(1);
        position.z = data.position(2);
        transform.position = position;

        Quaternion rotation;
        rotation.x = data.rotation(0);
        rotation.y = data.rotation(1);
        rotation.z = data.rotation(2);
        rotation.w = data.rotation(3);
        transform.rotation = rotation;
    }
}

The PlayerData :

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

(System.Serializable)
public class PlayerData
{
    public float() position;
    public float() rotation;
    
    public PlayerData(Player player)
    {
        position = new float(3);
        position(0) = player.transform.position.x;
        position(1) = player.transform.position.y;
        position(2) = player.transform.position.z;

        rotation = new float(4);
        rotation(0) = player.transform.rotation.x;
        rotation(1) = player.transform.rotation.y;
        rotation(2) = player.transform.rotation.z;
        rotation(3) = player.transform.rotation.w;
    }
}

And using the Saving in a script at some point :

IEnumerator SaveAfterSomeTime()
    {
        yield return new WaitForSeconds(15f);

        player.GetComponent<Player>().SavePlayer();
        StartCoroutine(fadeInOutSaveGame.OverAllTime(5f));
    }

And loading back from the main menu button click event :

public void ClickLoadGameDialog(string ButtonType)
        {
            if (ButtonType == "Yes")
            {
                loading = true;
                newGameDialog.SetActive(false);
                StartCoroutine(sceneFader.FadeAndLoadScene(SceneFader.FadeDirection.In, _newGameButtonLevel)); 
            }

            if (ButtonType == "No")
            {
                GoBackToMainMenu();
            }
        }

When I’m saving I see it’s creating a bin file on the hard disk.
When loading I see it’s getting other values of the position of the player.
But still it’s loading and the player is in the default position and rotation and not when where it was when saving.

The script Player is attached to my Player for testing I just added a 3D Cube and attached the script Player to the Cube but same thing. I changed the cube position saved but when loading the cube is starting from it’s original position and not from where it was when saving.

Something wrong with the SavePlayer method ? Or when loading ?