Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

SerializationException: Unable to load type required for deserialization

Discussion in 'Editor & General Support' started by deekpyro, Aug 13, 2020.

  1. deekpyro

    deekpyro

    Joined:
    Oct 16, 2012
    Posts:
    71
    I'm updating a Unity 2018 project to Unity 2019 and deserialization with a BinaryFormatter breaks with assets we had created in Unity 2018.

    Here's the line:
    Code (csharp):
    1. LevelType levelType = (LevelType)bFormatter.Deserialize(stream);
    And the error:
    System.Runtime.Serialization.SerializationException: Unable to load type LevelType required for deserialization.

    LevelType is a simple enum defined in the same file. Is there a change with .NET that causes this issue going from 2018 to 2019? Any ideas how I can fix this or at least step over this enum? Since we use this with player data in our production environment I want to maintain compatibility for the time being (and in the future I will move away from BinaryFormatter).
     
  2. deekpyro

    deekpyro

    Joined:
    Oct 16, 2012
    Posts:
    71
    Thanks to the help of someone on Twitter I discovered the assembly name where these types are defined changed. I was able to write a SerializationBinder to use the right types. Anyone changing assemblies and using a BinaryFormatter should be careful.