Well, i have a problem serializing an array, this are the funtions of Load/Save inside of a class inherited of Monobehavior
using UnityEngine;
using System.Collections;
using System;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
public class SaveLoadControl : MonoBehaviour {
public float life;
public float time;
public int score;
public int stageEnded;
public int deaths;
public void Save(int level,bool record)
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create(Application.persistentDataPath + "/userData.dat");
PlayerData data = new PlayerData ();
if (record) {
data.life[level] = life;
data.time[level] = time;
data.score[level] = score;
}
data.stageEnded[level] = stageEnded;
data.deaths = deaths;
bf.Serialize (file, data);
file.Close ();
}
public void Load(int level)
{
if (File.Exists (Application.persistentDataPath + "/userData.dat")) {
BinaryFormatter bf = new BinaryFormatter ();
FileStream file = File.Open (Application.persistentDataPath + "/userData.dat", FileMode.Open);
PlayerData data = (PlayerData)bf.Deserialize (file);
file.Close ();
life = data.life [level];
time = data.time [level];
score = data.score [level];
stageEnded = data.stageEnded [level];
deaths = data.deaths;
}
}
}
[Serializable]
class PlayerData
{
public float[] life;
public float[] time;
public int[] score;
public int[] stageEnded;
public int deaths;
public PlayerData()
{
life = new float[36];
time = new float[36];
score = new int[36];
stageEnded= new int[36];
}
}
The problem is when a save for example level 1, its everything working fine, but when i save level 2 after saving level 1, the data of level 1 its overwritten by 0, and level 2 its alright, i checked this with debug.log, any ideas?.