Why Does My Serialization Load Feature Not Work??

I created a test application game that essentially SHOULD doe two things:

  1. On first opening the game, a timer starts going. Once I press “Save” that time should be saved by Serialization to a file. Once I press “LOAD”, the previously saved Time should come back, and the timer should continue counting from that saved time onwards, and so forth…

  2. A random shape (rectangle) will increase in size at different time intervals. SO that over the many times I play this game, the minutes should start adding up and the rectangle with ‘GROW UP’ in a sense.

I have the code written down, but the problem is this:

The save feature works. But when I press LOAD, the saved time appears for about one frame, then resets to ZERO and the timer continues onwards from ZERO… so no real progress is saved. Can anybody help me please?


using UnityEngine;
using System.Collections;
using System;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

public class MultiplyBehaviour : MonoBehaviour {

public static MultiplyBehaviour mb;

public int timeAlive;			// a counter for how long the object has been alive (while application is running)
public double secondsCounter;	// a tracker for seconds per minute
public float scale;


void Start () {
//	if(scale < 1.0f)
//		scale = 1.0f;

//	transform.localScale = new Vector3(scale, scale, scale);
}

void Update () {

// Counter Controls
			if(secondsCounter >= 60){
				timeAlive++;                             // counts minutes
				secondsCounter = 0.0f;
			}
			secondsCounter += 1.0f * Time.deltaTime;
			
			if(secondsCounter >= 58){
				transform.localScale += new Vector3(1.0f, 1.0f, 1.0f) * Time.deltaTime;
				scale += 0.01f;                    // change size of rectangle
			}
}

public void Save()
{
	BinaryFormatter bf = new BinaryFormatter();
	FileStream file = File.Create(Application.persistentDataPath + "/first_test.dat");
	
	HoldMyData hmd = new HoldMyData();
	hmd.count = timeAlive;
	hmd.seconds = secondsCounter;
	hmd.scale = scale;
	
	bf.Serialize(file, hmd);
	file.Close();
}

void OnGUI()
{
	if(GUI.Button(new Rect(10, 120,100,30), "SAVE"))
	{
		Save();
		Application.Quit();
	}
	GUI.Label(new Rect(10, 40, 200, 30), "SecondCount: " + secondsCounter);
	GUI.Label(new Rect(10, 80, 200, 30), "Minutes: " + timeAlive);
}

}

[Serializable]
class HoldMyData {

public int count;
public double seconds;
public float scale;
}


And the second script is:


using UnityEngine;
using System.Collections;
using System;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

public class TitleScreen : MonoBehaviour {

MultiplyBehaviour mb;

void Start () 
{
		mb = GetComponent<MultiplyBehaviour>();
}
	
public void Load()
{
	if(File.Exists(Application.persistentDataPath + "/first_test.dat"))
	{
		BinaryFormatter bf = new BinaryFormatter();
		FileStream file = File.Open(Application.persistentDataPath + "/first_test.dat", FileMode.Open);
		HoldMyData hmd = (HoldMyData)bf.Deserialize(file);
		file.Close();
		
		mb.timeAlive = hmd.count;
		mb.secondsCounter = hmd.seconds;
	}
	//else
	//{
	//Application.LoadLevel ("SceneMain")
	//}
}

void OnGUI()
{
	if(GUI.Button(new Rect(10, 100,100,30), "LOAD"))
	{			
		Load();
		Application.LoadLevel ("SceneMain");
	}
}

}


Uhm, you call LoadLevel right after your “Load” method. That will destroy all objects in the current scene. So after the scene has been loaded all objects have the state which they had when you created the scene inside the editor. You might want to call your Load method inside Start, so when the scene is loaded it will read the values in the beginning.

We don’t have a full picture of your setup so this is just a shot in the dark.