Problem with serialized array

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?.

So what is happening here is

  1. When Ever you are saving level , you are only saving that level Only eg. when you save Level 2 … say Life[2]=20; You save that. and your Life[1]=0 (Default Value).
  2. But What about saving Level 1 Data again ? You are forgetting that.

Hope You GET this…

In case you dint … Before saving any level save all the previous one too.

i.e How to Save Level 2

  1. 1st load the saved file

  2. now u have player Data That was saved in file (i.e Level 1).

  3. Store this data.Life[0] in temp variable. (For all previous Level data use For Each Loop) or Store Directly in the current Reference

  4. Now save this Current Reference Of PlayerData in File. This Data has both Level 1 and Level 2 details.

If you having problem understanding that .Tell me i will help you out