- Home /
Load() not calling the the binaryformatter = file.create
I'm not sure that i've phrased this question properly but I hope this clarifies. I will post the three referenced codes in this order; "DataManger," (which also has the private class inside it PlayerData which is serializable) "Start_Screen_UIManager," and "ScoreManager"
I'm working on a save and load funtion.
DataManager Exists in the intital load of the entire game and then has a don't destroy on load funtion so that it can communicate with a bunch of other stuff later on.
Start_Screen_UIManager, exists in the first Scene but not in the second scene so that the UI Elements (buttons, images etc) don't persist into the second scene.
ScoreManager is a prefab which is in both the first and second scene but in the awake function will destroy itself if there is already another instance of it (in case while i'm editing I decide to start out in the second scene)
Here's how it should work. ScoreManager in gameplay will acquire (from an unreference script and gameObject) the newest high score and post that to the Start_Screen_UIManger (this part works) and will replace that highscore if another score is higher on later gameplays (also works). Where it goes wrong, is when I try to implement the DataManager.
I watched the older live training session on data persistence and the typing of the code works great and comes back with no errors when applied to my scripts and gameObjects).
I have the Save() Method being called OnDisable() of the DataManager and the Load() Method being call Awake() of the of the DataManager. So it should load. I would expect it to have nothing to load on my first play but when I stop the game and start it again it says the object i'm referencing is improperly referenced (line 58 of the DataManager or if the lines don't populate that last line of Data Manager before the "}".
All three objects holding these scripts are available in the first scene when it loads. Here are my scripts, please let me know what I'm not understanding or how I can better reference the GameObject holding the script.
using System.Collections; using System.Collections.Generic; using UnityEngine; using System; using System.Runtime.Serialization.Formatters.Binary; using System.IO;
public class DataManager : MonoBehaviour {
public static DataManager dataManager;
public void Awake()
{
{
if (dataManager == null)
{
DontDestroyOnLoad(gameObject);
dataManager = this;
}
else if (dataManager != null)
{
Destroy(gameObject);
}
}
Load();
}
public void OnDisable()
{
Save();
}
public void Save()
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create(Application.persistentDataPath + "/playerInfo.dat");
PlayerData data = new PlayerData();
data.score = Start_Screen_UIManager.ssUIManager.savedHighScore;
Load() not calling the the binaryformatter = file.create
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();
Start_Screen_UIManager.ssUIManager.savedHighScore = data.score;
}
}
}
[Serializable] class PlayerData { public int score;
}
using System.Collections; using System.Collections.Generic; using UnityEngine.UI; using UnityEngine;
public class Start_Screen_UIManager : MonoBehaviour {
public static Start_Screen_UIManager ssUIManager;
public Text savedHighScoreText;
public int savedHighScore = 0;
private void Awake()
{
if (ssUIManager == null)
{
ssUIManager = this;
}
else if (ssUIManager != this)
{
Destroy(gameObject);
}
}
public void Update()
{
if (savedHighScore < ScoreManager.scoreManager.newHighScore)
{
savedHighScore = ScoreManager.scoreManager.newHighScore;
savedHighScoreText.text = "High Score: " + savedHighScore;
}
else
{
savedHighScoreText.text = "High Score: " + savedHighScore;
}
}
}
using System.Collections; using System.Collections.Generic; using UnityEngine.SceneManagement; using UnityEngine;
public class ScoreManager : MonoBehaviour {
public static ScoreManager scoreManager;
public int newHighScore = 0;
private void Awake()
{
if (scoreManager == null)
{
DontDestroyOnLoad(gameObject);
scoreManager = this;
}
else if (scoreManager != this)
{
Destroy(gameObject);
}
}
private void Update()
{
Scene currentScene = SceneManager.GetActiveScene();
string sceneName = currentScene.name;
if (sceneName != "Play_Scene")
{
return;
}
else if (sceneName == "Play_Scene")
{
if (newHighScore < UIManager.uIManager.newHighScore)
{
newHighScore = UIManager.uIManager.newHighScore;
}
}
}
}
Your answer
Follow this Question
Related Questions
Is there a list of types binaryformatter can save? 2 Answers
Build not working on some saves for binary formatter but works on others 0 Answers
Save and Load in the build 0 Answers
Which types are actually serializable? Is the documentation incorrect or am I? 3 Answers
Saving and loading data from file 0 Answers