Saving doesn't work after Build
Hello! I have a problem with implementing a level unlock system to my game, the code works ingame, it loads and saves, but after i close the builded game and hit load, it doesn't do anything.
Here is the saving script. thanks the help in advance!
using UnityEngine;
using System.Collections;
using System;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
public class Saving : MonoBehaviour {
//This system is good because you cannot find the file in the game folder
//and change variables from there to cheat
public static Saving saveController;
//This is the variable from int the game,
//Make a reference to the object with the
//script with the variables you want to
//manage, or put all the variables you want to manage in here
private PlayerSave playerSave;
private void Start()
{
playerSave = GameObject.Find("PlayerSave").GetComponent<PlayerSave>();
}
//Singleton manager
private void Awake()
{
if(saveController == null)
{
DontDestroyOnLoad(gameObject);
saveController = this;
}
else if (saveController != this)
{
Destroy(gameObject);
}
}
public void Save()
{
// creates a binary formatter & a file;
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create(Application.persistentDataPath + "/playerInfo.dat");//Does not have to be .dat, but is what I went with
// creates a object to save the data to
PlayerData data = new PlayerData();
data.CreatedLevelIndex = playerSave.level;
data.CreatedLevel2unlock = playerSave.level2unlock;
data.CreatedLevel3unlock = playerSave.level3unlock;
// writes the object to the file and closes it
bf.Serialize(file, data);
file.Close();
}
public void Load()
{
if(File.Exists(Application.persistentDataPath + "/playerInfo.dat"))
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + "/playerInfo.dat", FileMode.Open);
PlayerData data = (PlayerData)bf.Deserialize(file);
file.Close();
playerSave.level = data.CreatedLevelIndex;
playerSave.level2unlock = data.CreatedLevel2unlock;
playerSave.level3unlock = data.CreatedLevel3unlock;
}
}
public void Delete()
{
if (File.Exists(Application.persistentDataPath + "/playerInfo.dat"))
{
File.Delete(Application.persistentDataPath + "/playerInfo.dat");
}
}
}
[Serializable]
class PlayerData
{
//Variables that will be in the created file
public int CreatedLevelIndex;
public bool CreatedLevel2unlock;
public bool CreatedLevel3unlock;
}
Comment
Your answer
Follow this Question
Related Questions
Saveing and Loading Problem 0 Answers
Problem with saving player data 0 Answers
PlayerPrefs Not Saving When I Click the Button. 1 Answer