- Home /
Serialization Issue (Data not updating)
I'll cut to the chase since the problem seems fairly simple and I'm guessing I'm just not experienced to see what I'm doing wrong. I followed a Unity tutorial on serialization and it worked great. Now I'm trying my hand at it and the values don't seem to be saving. Here are the lines I've narrowed my problem down to.
File.Delete(Application.persistentDataPath + "/PlayerCount.dat");
FileStream newFile = File.Create(Application.persistentDataPath + "/PlayerCount.dat");
PlayerCount data = new PlayerCount();
data.currentPlayerCount = data.currentPlayerCount + 1;
Debug.Log(data.currentPlayerCount + 1); //This always = 2
Debug.Log(data.currentPlayerCount); //This always = 1
bf.Serialize(newFile,data);
SaveParameters (pCountData.currentPlayerCount);
newFile.Close();
Line 5 always = 1, and line 6 always = 2. Shouldn't line 5 = Line 6?
Answer by GameVortex · Sep 08, 2015 at 06:54 AM
The two lines are not equal because you log out two different things.
You create initialize the PlayerCount data on line 3. At that point the variables in PlayerCount is initialized to default values. data.currentPlayerCount initializes to have a value of 0.
On line 4 you add 1 to data.currentPlayerCount (= 0 + 1), making the value now 1.
Then on line 5 you log out the data.currentPlayerCount + 1 which is 2. You are not using the = sign anywhere on that line meaning that no value changes, you just log out what data.currentPlayerCount added with 1 is.
Then on line 6 you log out what data.currentPlayerCount is which is still 1, from the added value on Line 4.
PS: I am assuming you mixed up your note at the bottom because your comments in your code says the reverse (Line 5 = 2 and Line 6 = 1).