Variables changes even on pause
Hello, i have a script for storing serialized data. But some variables are always setting equal to each other. For example statLevels in progressData are always equal statLevelsStats in stats, even if game on pause in Unity.
Here is a code:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
[System.Serializable]
public class ProgressContainer {
[System.Serializable]
public class progressData
{
public int lastCheckPoint;
public int level;
public List<int> pickedUpMonets = new List<int>();
public bool notSaved = true;
public int coins;
public int skates;
public int[] statLevels = new int[4];
public int[] skillLevels = new int[4];
}
[System.Serializable]
public class Settings
{
public bool watchedYYYC;
public bool helpersHidden;
public float audioMusicVolume = 1;
public float audioSoundsVolume = 1;
public bool mute;
public Language language;
}
[System.Serializable]
public class Stats
{
public bool watchedDialog = false;
public int coins;
public int skates;
public int energy;
public int lastLevel;
public int lastOpenedLevel = -1;
public int[] skillLevels = new int[4];
public int[] statLevelsStats = new int[4];
public bool hardcore;
public int lives = 5;
public bool gameStarted = false;
}
public List<progressData> progress = new List<progressData>();
public Stats stats = new Stats();
public Settings settings = new Settings();
// Use this for initialization
public void Save (string path) {
BinaryFormatter bf = new BinaryFormatter ();
path += "/playerInfo.dat";
FileStream file = File.Create (path);
bf.Serialize (file, this);
file.Close ();
Debug.Log ("Saved : "+path);
}
public static ProgressContainer Load(string path)
{
path += "/playerInfo.dat";
if (!File.Exists (path))
return new ProgressContainer();
BinaryFormatter bf = new BinaryFormatter ();
FileStream file = File.Open (path,FileMode.Open);
ProgressContainer container;
container = (ProgressContainer)bf.Deserialize (file);
file.Close ();
return container;
}
public void NewGame(bool hardcore)
{
progress.Clear();
stats = new Stats ();
stats.hardcore = hardcore;
stats.gameStarted = true;
if(Game.instance.unlockAllLevels)
{
stats.lastLevel = 99;
stats.lastOpenedLevel = 99;
}
Game.instance.Save ();
}
}
Can it be because of Serialization? I Marked all functions in other scripts where changing variables in ProgressContainer with Debug.Log(). But they are not Calling.
It may not be the solution, but I note you use List<OneOfYourClasses>
as if it was already a serializable type, whereas it's not. Remember the generic class List<>
is only serializable with built-in types as a generic parameter (scalars, i.e int, bool, float, double, string, ...) not your serializable class, so for example List<int>
is serializable but List<progressData>
is not, you have to create a derived class from your special List to make it serializable: [System.Serializable] public class ProgressDataList : List<progressData> { }
And declare the field like this: public ProgressDataList progress = new ProgressDataList();
Your answer
Follow this Question
Related Questions
How to save many instantiated objects position/rotation at runtime??? 2 Answers
Saving monster class List problem. 0 Answers
Best way to serialize a big array of a class 2 Answers
Saving Data (please help) 0 Answers