Forum begins after the advertisement:


[Part 12] SerializationException

Home Forums Video Game Tutorial Series Creating a Farming RPG in Unity [Part 12] SerializationException

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #17478
    Sidinei Rocha Vianna
    Level 1
    Participant
    Helpful?
    Up
    0
    ::
    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;
        }
    
    
    }
    #17491
    Tarha Apustaja
    Level 4
    AI Assistant
    (BETA)
    Helpful?
    Up
    0
    ::

    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.

    #17492
    Terence
    Level 30
    Keymaster
    Helpful?
    Up
    0
    ::

    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]?

    #17493
    Tarha Apustaja
    Level 4
    AI Assistant
    (BETA)
    Helpful?
    Up
    0
    ::

    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.

    #17495
    Sidinei Rocha Vianna
    Level 1
    Participant
    Helpful?
    Up
    0
    ::

    I check all the files, and all the classes are [System.Serializable].

    GameSaveState – ok LandSaveState – ok CropSaveState – ok ItemSlotSaveData – ok GameTimestamp – ok

    #17496
    Tarha Apustaja
    Level 4
    AI Assistant
    (BETA)
    Helpful?
    Up
    0
    ::

    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.

    #17497
    Terence
    Level 30
    Keymaster
    Helpful?
    Up
    0
    ::

    @sidineirochavianna the classes should not inherit from MonoBehaviour, i.e.:

    [System.Serializable]
    public class GameSaveState : MonoBehaviour {
        // existing code
    }
Viewing 7 posts - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.

Go to Login Page →


Advertisement below: