Unity-Technologies/UnityCsReference · error · ArgumentException

The type must be serializable.

Error message

The type must be serializable.

What it means

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]).

Source

Thrown at Editor/Mono/ProjectBrowser/SavedSearchFilter.cs:522

                SavedFilter s = m_SavedFilters[i];
                text += string.Format(": {0} ({1})({2})({3}) ", s.m_Name, filterId, s.m_Depth, s.m_PreviewSize);
            }
            return text;
        }
    }


    // Provides a method for performing a deep copy of an object.
    // Binary Serialization is used to perform the copy.
    // Reference Article http://www.codeproject.com/KB/tips/SerializedObjectCloner.aspx
    internal static class ObjectCopier
    {
        // Perform a deep Copy of the source object.
        public static T DeepClone<T>(T source)
        {
            if (!typeof(T).IsSerializable)
            {
                throw new ArgumentException("The type must be serializable.", "source");
            }

            // Don't serialize a null object, simply return the default for that object
            if (Object.ReferenceEquals(source, null))
            {
                return default(T);
            }

            var serializer = new DataContractSerializer(typeof(T));
            Stream stream = new MemoryStream();
            using (stream)
            {
                serializer.WriteObject(stream, source);
                stream.Seek(0, SeekOrigin.Begin);
                return (T)serializer.ReadObject(stream);
            }
        }
    }

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Ensure the type T is decorated with [Serializable] or [DataContract] attribute.
  2. 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).
  3. For SavedSearchFilter specifically, use the provided serialization mechanisms rather than calling DeepClone on incompatible types.
  4. 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.

Example fix

// before
var copy = ObjectCopier.DeepClone(myNonSerializableObject);
// after
[Serializable]
public class MyData { /* ... */ }
var copy = ObjectCopier.DeepClone(myData);
Defensive patterns

Strategy: validation

Validate before calling

if (typeof(T).IsSerializable)
    var copy = ObjectCopier.DeepClone(source);
else
    throw new InvalidOperationException($"{typeof(T)} is not serializable; use an alternative clone method.");

Type guard

static bool IsSerializableType<T>() => typeof(T).IsSerializable;

Prevention

When it happens

Trigger: 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.

Common situations: 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).

Related errors


AI-assisted analysis of Unity-Technologies/UnityCsReference@225b0fbdb5 (2026-08-13). Data as JSON: /api/errors/280fc01247f659c5. Report an issue: GitHub.