- Home /
nice Save but wrong load
Hello all! I have a little problem with save/load script.
I have this script
using UnityEngine;
using System.Collections;
using System;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Text;
public class SaveLoadTest : MonoBehaviour {
public string FileSaveName;
public float SomeFloat1;
public float SomeFloat2;
public float SomeFloat3;
// Use this for initialization
void Awake () {
if(File.Exists(Application.persistentDataPath + "\\" + FileSaveName))
{
Load();
}
}
// Update is called once per frame
void Update () {
if(Input.GetMouseButtonDown(0))
{
Save();
}
}
public void Save()
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create(Application.persistentDataPath + "\\" + FileSaveName);
LoadClass settingsData = new LoadClass();
settingsData.SomeFloat1= SomeFloat1;
settingsData.SomeFloat2= SomeFloat2;
settingsData.SomeFloat3= SomeFloat3;
bf.Serialize(file,settingsData);
file.Close();
}
public void Load()
{
if(File.Exists(Application.persistentDataPath + "\\" + FileSaveName))
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + "\\" + FileSaveName, FileMode.Open);
LoadClass settingsData = (LoadClass)bf.Deserialize(file);
file.Close();
SomeFloat1= settingsData.SomeFloat1;
SomeFloat2= settingsData.SomeFloat2;
SomeFloat3= settingsData.SomeFloat3;
}
}
[Serializable]
class LoadClass
{
public float SomeFloat1;
public float SomeFloat2;
public float SomeFloat3;
}
}
its works perfect!
BUT!
When i try Serialize class from another script
For example like this :
using UnityEngine;
using System.Collections;
using System;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Text;
public class SaveLoadTest1 : MonoBehaviour {
public string FileSaveName;
public float SomeFloat1;
public float SomeFloat2;
public float SomeFloat3;
// Use this for initialization
void Awake () {
if(File.Exists(Application.persistentDataPath + "\\" + FileSaveName))
{
Load();
}
}
// Update is called once per frame
void Update () {
if(Input.GetMouseButtonDown(0))
{
Save();
}
}
public void Save()
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create(Application.persistentDataPath + "\\" + FileSaveName);
LoadClass settingsData = new LoadClass();
settingsData.SomeFloat1= SomeFloat1;
settingsData.SomeFloat2= SomeFloat2;
settingsData.SomeFloat3= SomeFloat3;
bf.Serialize(file,settingsData);
file.Close();
}
[Serializable]
class LoadClass
{
public float SomeFloat1;
public float SomeFloat2;
public float SomeFloat3;
}
}
When i "saved"(Serialize) with help of second script SaveLoadTest1(for example) my first script SaveLoadTest(for example) cant load(Deserialize class) and show me error :
InvalidCastException: Cannot cast from source type to destination type.
When i use only first script SaveLoadTest for save and load (Seria. and DeSeria.) all works nice!
Anybody can help? Explain why its happen....
Thx for your attention! (sorry for my English)
Your answer
Follow this Question
Related Questions
How to save and load any data type? 1 Answer
How to script a Save/Load option on IPhone? 1 Answer
Saving Game Problem 1 Answer
PlayerPrefsX - Not Saving? 1 Answer
Game and Level State Logic 1 Answer