Problems when trying to save data using binary files

Hello.
I’ve been having problems with binary saving. I’m a beginner to data-saving and have followed this tutorial:

Which worked fine, but in the second part of the tutorial:

Things did not go so well. I was trying to do the part where he turns the loading void into an int (sorry, I don’t know what the terms for this are), but the code didn’t work anymore. What I’m trying to do is:

I have a 2D game where a character walks on top of a circle, like a small planet (but instead of making the character walk on the planet, I make the planet rotate behind him to give the illusion of movement), and by pressing O, you save the rotation of the circle, and by pressing P, you load it, and the rotation of the planet sets to the value stored. This is my code:

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

public class Save : MonoBehaviour {

    public GameObject Planeta; //Planeta is the planet

    static string path = "/Saves/";
    static string filename = "saveData";
    static string ext = ".sz";

    void Start () {

        Planeta = GameObject.FindWithTag ("Planeta");

    }

    //This is the code to save the rotation.

    public static void SavePlanetInfo(float planDir) {
      
        BinaryFormatter bf = new BinaryFormatter();
        FileStream file = File.Create (Application.dataPath + path + filename + ext);
        bf.Serialize (file, planDir);
        file.Close ();

    }

    //This is the loading code

    public static float LoadPlanetInfo() {
      
        if (File.Exists (Application.dataPath + path + filename + ext)) {

            BinaryFormatter bf = new BinaryFormatter();
            FileStream file = File.Open (Application.dataPath + path + filename + ext, FileMode.Open);

            float prot = (float)bf.Deserialize (file);
            file.Close ();
            return prot;

        } else {
          
            return 0;

        }

    }
      
    // Quicksave controls

    void Update() {

        if (Input.GetKeyDown (KeyCode.O)) {

            SavePlanetInfo (Planeta.transform.rotation.z);

        }

        if (Input.GetKeyDown (KeyCode.P)) {

            Planeta.transform.rotation = Quaternion.Euler (new Vector3 (0, 0, LoadPlanetInfo ()));

        }

    }

}

No compiler errors appear, and I have no idea what to do. I have a backup of a previews build if I can’t get it to work, but if anyone could help me, I’d be immensely grateful.

No compiler errors.

So what happens? What runtime stuff occurs? Have you ran the debugger and stepped through the code?

When I press the load button the rotation of the planet just goes back to 0. Nothing else happens.

From my debugging attempt, it seems like the problem is not with the saving feature, but with the loading, but this is all I discovered.

        if (Input.GetKeyDown(KeyCode.O))
        {

            SavePlanetInfo(Planeta.transform.rotation.z);

        }

        if (Input.GetKeyDown(KeyCode.P))
        {

            Planeta.transform.rotation = Quaternion.Euler(new Vector3(0, 0, LoadPlanetInfo()));

        }

This line here:

SavePlanetInfo(Planeta.transform.rotation.z);

That ‘z’ value is not the roll value. ‘rotation’ is a Quaternion, which contains 4 fields x,y,z,w. These are complex numbers.

You should be saying:

SavePlanetInfo(Planeta.transform.eulerAngles.z);

Oh, ok. That makes sense. I’m going to try this out and when I do I’ll update the thread.

It worked! Really stupid mistake honestly. Thank you very much!