0
\$\begingroup\$

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 ?

\$\endgroup\$
  • \$\begingroup\$ I would put a debugger breakpoint into Player.LoadPlayer to see if it gets called at all, and when it does get called proceed to see what data I get from SaveSystem.LoadPlayer();. Can you do that and report back? \$\endgroup\$ – Philipp Sep 16 at 13:00
  • \$\begingroup\$ Do you even call the player.LoadPlayer method? I can't find anywhere in your code where you do. All your click handler seems to be doing is start a coroutine ceneFader.FadeAndLoadScene and that's it. Perhaps that coroutine does, but its sourcecode is missing from your question. \$\endgroup\$ – Philipp Sep 16 at 13:03
  • \$\begingroup\$ @Philipp You right about not calling player.LoadPlayer I removed it since I tested it with a cube so I added it back and I used a break point and it's getting to the LoadPlayer method in the Player class and when I'm looking on the data variable and on the line that assign to the transform(Cube) back the loaded position : transform.rotation = rotation; I see the new position values. but still the Cube is stay in the same place. Something else prevent from it to move or something. I will try to figure it out. A bit strange since the transform get the new loaded position. \$\endgroup\$ – Daniel Lip Sep 16 at 15:00
  • \$\begingroup\$ @Philipp I think the problem is that first it's changing the cube position to the new position I check with a break point on the line transform.position = position; and then it's loading the scene. Maybe I should first waiting for the scene to be loaded and then to make the loading line ? This is where I added the LoadPlayer but it's making the loading before the scene finished loading. StartCoroutine(sceneFader.FadeAndLoadScene(SceneFader.FadeDirection.In, _newGameButtonLevel)); player.GetComponent<Player>().LoadPlayer(); \$\endgroup\$ – Daniel Lip Sep 16 at 15:10
  • 1
    \$\begingroup\$ When you managed to solve your problem on your own, then you should write an answer to your own question where you explain how you solved it. That way people can see that it's solved and how you solved it. \$\endgroup\$ – Philipp Sep 16 at 15:45

Your Answer

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

Browse other questions tagged or ask your own question.