Forum begins after the advertisement:
[Part 12] SerializationException
Home › Forums › Video Game Tutorial Series › Creating a Farming RPG in Unity › [Part 12] SerializationException
- This topic has 2 replies, 3 voices, and was last updated 3 weeks, 1 day ago by
Terence.
-
AuthorPosts
-
March 10, 2025 at 7:19 pm #17478::
SerializationException: Type 'UnityEngine.MonoBehaviour' in Assembly 'UnityEngine.CoreModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable.
when I go to sleep the game crashes.
using System.Collections; using System.Collections.Generic; using System.IO; using System.Runtime.Serialization.Formatters.Binary; using UnityEngine; public class SaveManager : MonoBehaviour { static readonly string FILEPATH = Application.persistentDataPath + "/Save.save"; public static void Save(GameSaveState save) { //Save as JSON //string json = JsonUtility.ToJson(save); //File.WriteAllText(FILEPATH, json); //Save as binary file using (FileStream file = File.Create(FILEPATH)) { new BinaryFormatter().Serialize(file, save); Debug.Log("SaveManager(save) funcionando"); } } public static GameSaveState Load() { GameSaveState loadedSave = null; //JSON /* if (File.Exists(FILEPATH)) { string json = File.ReadAlltext(FILEPATH); loadedSave = JsonUtility.FromJson<GameSaveState>(json); }*/ //Bynary method //Check if the file exist if (File.Exists(FILEPATH)) { Debug.Log("O file Save existe"); //Open it up and deserialize using (FileStream file = File.Open(FILEPATH, FileMode.Open)) { object loadedData = new BinaryFormatter().Deserialize(file); //cast the deserialized object as a GameSaveState loadedSave = (GameSaveState)loadedData; } } return loadedSave; } }
March 10, 2025 at 9:50 pm #17491::The reference information suggests that to resolve the SerializationException, you need to mark your classes with [System.Serializable] attribute. Specifically, all classes involved in saving (GameSaveState, LandSaveState, CropSaveState, etc.) must be marked with [System.Serializable] to be compatible with BinaryFormatter serialization.
March 10, 2025 at 9:52 pm #17492::Just to add on to what Tarha said, you mark it like this:
[System.Serializable] public class GameSaveState { ... }
You need to mark all the classes that Tarha mentions.
@Tarha can you provide a list of all the classes in the project we need to mark as
[System.Serializable]
?March 10, 2025 at 9:56 pm #17493::The error occurs because not all classes involved in serialization are marked with [System.Serializable]. You need to add the [System.Serializable] attribute to all classes that will be serialized, including:
- GameSaveState
- LandSaveState
- CropSaveState
- ItemSlotSaveData
- GameTimestamp
Example:
[System.Serializable] public class GameSaveState { // existing code }
This will resolve the SerializationException by marking the classes as serializable for binary serialization.
March 10, 2025 at 10:26 pm #17495::I check all the files, and all the classes are [System.Serializable].
GameSaveState – ok LandSaveState – ok CropSaveState – ok ItemSlotSaveData – ok GameTimestamp – ok
March 10, 2025 at 10:30 pm #17496::The reference information confirms that all the mentioned classes should indeed be marked with [System.Serializable], so your observation is correct. This attribute allows these classes to be serialized and saved/loaded in Unity’s save system.
March 10, 2025 at 10:57 pm #17497::@sidineirochavianna the classes should not inherit from MonoBehaviour, i.e.:
[System.Serializable] public class GameSaveState
: MonoBehaviour{ // existing code } -
AuthorPosts
- You must be logged in to reply to this topic.
Advertisement below: