Error:
SerializationException: Type 'UnityEngine.Transform' in Assembly 'UnityEngine.CoreModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable.
System.Runtime.Serialization.FormatterServices.InternalGetSerializableMembers (System.RuntimeType type) (at <437ba245d8404784b9fbab9b439ac908>:0)
System.Runtime.Serialization.FormatterServices+<>c__DisplayClass9_0.<GetSerializableMembers>b__0 (System.Runtime.Serialization.MemberHolder _) (at <437ba245d8404784b9fbab9b439ac908>:0)
System.Collections.Concurrent.ConcurrentDictionary`2[TKey,TValue].GetOrAdd (TKey key, System.Func`2[T,TResult] valueFactory) (at <437ba245d8404784b9fbab9b439ac908>:0)
System.Runtime.Serialization.FormatterServices.GetSerializableMembers (System.Type type, System.Runtime.Serialization.StreamingContext context) (at <437ba245d8404784b9fbab9b439ac908>:0)
System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitMemberInfo () (at <437ba245d8404784b9fbab9b439ac908>:0)
System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitSerialize (System.Type objectType, System.Runtime.Serialization.ISurrogateSelector surrogateSelector, System.Runtime.Serialization.StreamingContext context, System.Runtime.Serialization.Formatters.Binary.SerObjectInfoInit serObjectInfoInit, System.Runtime.Serialization.IFormatterConverter converter, System.Runtime.Serialization.SerializationBinder binder) (at <437ba245d8404784b9fbab9b439ac908>:0)
System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.Serialize (System.Type objectType, System.Runtime.Serialization.ISurrogateSelector surrogateSelector, System.Runtime.Serialization.StreamingContext context, System.Runtime.Serialization.Formatters.Binary.SerObjectInfoInit serObjectInfoInit, System.Runtime.Serialization.IFormatterConverter converter, System.Runtime.Serialization.SerializationBinder binder) (at <437ba245d8404784b9fbab9b439ac908>:0)
System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteArray (System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo objectInfo, System.Runtime.Serialization.Formatters.Binary.NameInfo memberNameInfo, System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo memberObjectInfo) (at <437ba245d8404784b9fbab9b439ac908>:0)
System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Write (System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo objectInfo, System.Runtime.Serialization.Formatters.Binary.NameInfo memberNameInfo, System.Runtime.Serialization.Formatters.Binary.NameInfo typeNameInfo) (at <437ba245d8404784b9fbab9b439ac908>:0)
System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Serialize (System.Object graph, System.Runtime.Remoting.Messaging.Header[] inHeaders, System.Runtime.Serialization.Formatters.Binary.__BinaryWriter serWriter, System.Boolean fCheck) (at <437ba245d8404784b9fbab9b439ac908>:0)
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize (System.IO.Stream serializationStream, System.Object graph, System.Runtime.Remoting.Messaging.Header[] headers, System.Boolean fCheck) (at <437ba245d8404784b9fbab9b439ac908>:0)
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize (System.IO.Stream serializationStream, System.Object graph, System.Runtime.Remoting.Messaging.Header[] headers) (at <437ba245d8404784b9fbab9b439ac908>:0)
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize (System.IO.Stream serializationStream, System.Object graph) (at <437ba245d8404784b9fbab9b439ac908>:0)
SaveSystem.SaveScene (SceneData sceneData) (at Assets/Code/SaveSystem.cs:15)
SceneData.SaveScene () (at Assets/Code/SceneData.cs:31)
NextScene+<LoadLevel>d__7.MoveNext () (at Assets/Code/NextScene.cs:44)
UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at <23a7799da2e941b88c6db790c607d655>:0)
SceneData:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SceneData : MonoBehaviour
{
public List<Transform> rooms;
public List<Transform> loot;
public List<Transform> enemy;
public void GameData()
{
foreach (GameObject r in GameObject.FindGameObjectsWithTag("Rooms"))
{
rooms.Add(r.GetComponent<Transform>());
}
foreach (GameObject l in GameObject.FindGameObjectsWithTag("Loot"))
{
loot.Add(l.GetComponent<Transform>());
}
foreach (GameObject e in GameObject.FindGameObjectsWithTag("Enemy"))
{
enemy.Add(e.GetComponent<Transform>());
}
}
public void SaveScene()
{
SaveSystem.SaveScene(this);
}
public void LoadScene()
{
GameData();
GameData data = SaveSystem.LoadScene();
rooms = data.rooms;
loot = data.loot;
enemy = data.enemy;
}
}
GameData:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class GameData
{
public List<Transform> rooms;
public List<Transform> loot;
public List<Transform> enemy;
public GameData (SceneData sceneData)
{
rooms = sceneData.rooms;
loot = sceneData.loot;
enemy = sceneData.enemy;
}
}
SaveSystem:
using UnityEngine;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
public static class SaveSystem
{
public static void SaveScene (SceneData sceneData)
{
BinaryFormatter formatter = new BinaryFormatter();
string path = Path.Combine(Application.persistentDataPath, "scene.Alden");
FileStream stream = new FileStream(path, FileMode.Create);
GameData data = new GameData(sceneData);
formatter.Serialize(stream, data);
stream.Close();
}
public static GameData LoadScene()
{
string path = Path.Combine(Application.persistentDataPath, "scene.Alden");
if (File.Exists(path))
{
BinaryFormatter formatter = new BinaryFormatter();
FileStream stream = new FileStream(path, FileMode.Open);
GameData data = formatter.Deserialize(stream) as GameData;
stream.Close();
return data;
}
else
{
UnityEngine.Debug.Log("Save file not found in" + path);
return null;
}
}
}