I get an error whenever I try and use the saveTerrainDatabase() method in my code below.
SerializationException: Type UnityEngine.MonoBehaviour in assembly UnityEngine, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null is not marked as serializable.
I’ve read from another question posted on unity answers that classes inheriting from monobehavior can be serialized (which I have done).
using UnityEngine;
using System.Collections;
using System.Xml;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System;
[System.Serializable]
public class terrainManager : MonoBehaviour {
public chunkManager[] chunks = new chunkManager[100];
private fileManager fm = new fileManager();
private bool isDataEncrypted = false;
public void Awake()
{
fm.initialize();
fm.createDirectory("Database"); //creates a file directory if one does not exist
if(fm.checkFile("Database/Terrain.dat") == true) //if file exists, use it
{
Debug.Log("File found");
loadTerrainDatabase();
}
else //else create a new file if there isn't one
{
Debug.Log("Creating new file");
fm.createFile("Database","Terrain","dat","empty terrain file",isDataEncrypted);
}
}
public void loadTerrainDatabase()
{
}
public void saveTerrainDatabase()
{
BinaryFormatter bf = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
bf.Serialize(ms,this);
fm.updateFile("Database","Terrain","dat",Convert.ToBase64String(ms.GetBuffer()),isDataEncrypted, false);
}
The error exception highlights line 41 whenever I double click the error in the console.
@LifeAlchemist an added benefit of using Unity Serializer, is that you get to see the world's 8th wonder - ala the world's longest LiNQ expression :D
– vexeWhat does it mean when it says serializable types in this part of the documentation then? It's really confusing. http://docs.unity3d.com/Documentation/ScriptReference/SerializeField.html
– Life_AlchemistIt means serializable by Unity into scenes etc.
– whydoidoit"Or you could make your class a normal one and just have a reference to it in your MonoBehaviour." Can you give a small snippet of how this would look or how it would work. Reason is of the bottom posting on this page: http://answers.unity3d.com/questions/341908/how-should-i-serialize-data-that-is-also-editable.html?sort=oldest
– EmeralLotusif anyone is interested i found this live training session on serialisation really helpful http://youtu.be/J6FfcJpbPXE
– davidc