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.