- Home /
Persistence of saving data not working
In my game I have the inventory which when saved links to the Datasaver which isn't destroyed between scenes. The reason for this is because its a space game so the inventory script doesn't work in the space scene between scenes. I've tried over and over to get it to work and I haven't really gotten anywhere. Below is the script for the DataSaver which is called first at the start of each scene.
public void load()
{
if (File.Exists(Application.persistentDataPath + "/playerInfo.dat"))
{
print("load");
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + "/playerInfo.dat", FileMode.Open);
PlayerData data = (PlayerData)bf.Deserialize(file);
remist2 = data.remist;
relime2 = data.relime;
reolive2 = data.reolive;
reaqua2 = data.reaqua;
file.Close();
Inventory.control.loading(remist2, relime2, reolive2, reaqua2);
}
}
The Debugs are to help me figure out which part is working during play time. Then the information is sent to inventory to update the variables there.
public void loading(int remist2, int relime2, int reolive2, int reaqua2)
{
remist = remist2;
relime = relime2;
reolive = reolive2;
reaqua = reaqua2;
print("loading to inv");
}
Later on the variables are saved to datasaver by sending the variables across to it
public void save()
{
print("save");
DataFile.controldata.save(remist, relime, reolive, reaqua);
}
When they arrive datasaver saves them and then after that when the scene changes and changes back it reloads the data from the file except it isn't come up with the values. But it is working because I noticed when I stopped the game and replayed it, the values were there but that's the only instance that they have saved
public void save(int remist, int relime, int reolive, int reaqua)
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create(Application.persistentDataPath + "/playerInfo.dat");
PlayerData data = new PlayerData();
data.remist = remist;
data.relime = relime;
data.reolive = reolive;
data.reaqua = reaqua;
bf.Serialize(file, data);
file.Close();
}
Please ask if there is more information that is required
Answer by x4637x · Mar 31, 2018 at 05:22 AM
You might want to have [Serializable] attribute for your PlayerData class.
Yeah it is serialized
[Serializable]
class PlayerData
{
public int remist;
public int relime;
public int reolive;
public int reaqua;
public int fusionCount;
public int warpfuel;
public int monumentPiece;
public int mistCount;
public int limeCount;
public int oliveCount;
public int aquaCount;
public int galacticUnits;
public int ionCurrency;
public int traderCoin;
}
Please use the comment if you do not answer this question.
Answer by melsy · Apr 02, 2018 at 11:37 AM
Check out my post here for a full save load serialization setup with classes. Compare them and see where you missed something. https://answers.unity.com/questions/1463102/inventory-that-works-across-scenes.html?childToView=1463107#answer-1463107
Your answer
Follow this Question
Related Questions
Saving data. 1 Answer
Saving build number to a file inside build data folder 2 Answers
Multiple Cars not working 1 Answer
Saving Data 1 Answer
Distribute terrain in zones 3 Answers