{"record":{"id":"280fc01247f659c5","repo":"Unity-Technologies/UnityCsReference","slug":"the-type-must-be-serializable","errorCode":null,"errorMessage":"The type must be serializable.","messagePattern":"The type must be serializable\\.","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/ProjectBrowser/SavedSearchFilter.cs","lineNumber":522,"sourceCode":"                SavedFilter s = m_SavedFilters[i];\n                text += string.Format(\": {0} ({1})({2})({3}) \", s.m_Name, filterId, s.m_Depth, s.m_PreviewSize);\n            }\n            return text;\n        }\n    }\n\n\n    // Provides a method for performing a deep copy of an object.\n    // Binary Serialization is used to perform the copy.\n    // Reference Article http://www.codeproject.com/KB/tips/SerializedObjectCloner.aspx\n    internal static class ObjectCopier\n    {\n        // Perform a deep Copy of the source object.\n        public static T DeepClone<T>(T source)\n        {\n            if (!typeof(T).IsSerializable)\n            {\n                throw new ArgumentException(\"The type must be serializable.\", \"source\");\n            }\n\n            // Don't serialize a null object, simply return the default for that object\n            if (Object.ReferenceEquals(source, null))\n            {\n                return default(T);\n            }\n\n            var serializer = new DataContractSerializer(typeof(T));\n            Stream stream = new MemoryStream();\n            using (stream)\n            {\n                serializer.WriteObject(stream, source);\n                stream.Seek(0, SeekOrigin.Begin);\n                return (T)serializer.ReadObject(stream);\n            }\n        }\n    }","sourceCodeStart":504,"sourceCodeEnd":540,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/ProjectBrowser/SavedSearchFilter.cs#L504-L540","documentation":"ObjectCopier.DeepClone throws an ArgumentException when the type T is not marked as serializable. This internal utility performs deep cloning via DataContractSerializer, which requires the type to be serializable (decorated with [Serializable] or [DataContract]).","triggerScenarios":"Calling DeepClone with a type that lacks [Serializable] or [DataContract] attribute. The check is typeof(T).IsSerializable which inspects the compile-time type, not the runtime instance type.","commonSituations":"Attempting to deep-clone a Unity Object-derived type (GameObject, MonoBehaviour, ScriptableObject) which are not marked [Serializable] for this purpose. Also happens with types from third-party libraries that weren't designed for serialization. Common in editor tools that try to copy filter/search configurations (this method serves SavedSearchFilter).","solutions":["Ensure the type T is decorated with [Serializable] or [DataContract] attribute.","If the type cannot be made serializable, use an alternative cloning strategy (e.g., JSON serialization, manual copy, or Unity's JsonUtility for compatible types).","For SavedSearchFilter specifically, use the provided serialization mechanisms rather than calling DeepClone on incompatible types.","If T is a base type but the runtime instance is a derived non-serializable type, change T to the actual derived type or implement ICloneable manually."],"exampleFix":"// before\nvar copy = ObjectCopier.DeepClone(myNonSerializableObject);\n// after\n[Serializable]\npublic class MyData { /* ... */ }\nvar copy = ObjectCopier.DeepClone(myData);","handlingStrategy":"validation","validationCode":"if (typeof(T).IsSerializable)\n    var copy = ObjectCopier.DeepClone(source);\nelse\n    throw new InvalidOperationException($\"{typeof(T)} is not serializable; use an alternative clone method.\");","typeGuard":"static bool IsSerializableType<T>() => typeof(T).IsSerializable;","tryCatchPattern":null,"preventionTips":["Mark types with [Serializable] or [DataContract] before using DeepClone","Do not attempt to DeepClone Unity Object-derived types (GameObject, MonoBehaviour)","Use JsonUtility or manual copy for non-serializable types","For SavedSearchFilter, ensure the data model type is serializable"],"tags":["unity","editor","serialization","deep-clone","argument-validation"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}