# CopySerialized(Object, Object)

> Copies all serialized properties of a Unity Object from one object to another.

## Definition

* **Type:** Method
* **Namespace:** [UnityEditor](/engine/6000.6/script-reference/unityeditor.md)
* **Assembly:** UnityEditor.CoreModule

```csharp
public static void CopySerialized(Object source, Object dest)
```

### Parameters

**** (\[Object]\(/engine/6000.6/script-reference/unityengine/object)): The object to copy the properties from.**** (\[Object]\(/engine/6000.6/script-reference/unityengine/object)): The object to copy the properties to.

### Remarks

This method copies all serialized properties from the `source` object to the `dest` object, overwriting values that are currently in the properties of `dest`. The fields that are included follow the [script-serialization-rules](/engine/6000.6/manual/scripting/compilation-and-code-reload/script-serialization/rules.md).

This method is mostly useful when working with objects in assets. It is similar to [Object.Instantiate](/engine/6000.6/script-reference/unityengine/object/instantiate.md) in that it also copies the serialized properties of an object, but it overwrites an existing object, unlike `Instantiate` which always creates a new object.

Note that [Object.hideFlags](/engine/6000.6/script-reference/unityengine/object/hideflags.md) are not copied.

If the object being copied derives from [MonoBehaviour](/engine/6000.6/script-reference/unityengine/monobehaviour.md) or [ScriptableObject](/engine/6000.6/script-reference/unityengine/scriptableobject.md), then `dest` must either be the same type as `source`, or a base class of it. Otherwise, the types of `dest` and `source` must be equal.

Because the Unity serializer is used to copy the values, if the [ISerializationCallbackReceiver.OnBeforeSerialize](/engine/6000.6/script-reference/unityengine/iserializationcallbackreceiver/onbeforeserialize.md) method is implemented then it's invoked on the `source` object before copying. If the [ISerializationCallbackReceiver.OnAfterDeserialize](/engine/6000.6/script-reference/unityengine/iserializationcallbackreceiver/onafterdeserialize.md) method is implemented, it's invoked on the `dest` object after copying.

The following example shows how this method could be used to reset the properties of an object to match a 'reference' object, instead of manually resetting the properties to hardcoded values.

### Examples

```csharp
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

[CreateAssetMenu]
public class CopySerializedExample : ScriptableObject
{
    // Declare some serialized fields
    public int intValue;
    public string stringValue;
    public List<bool> listOfBools;

    #if UNITY_EDITOR
    [ContextMenu("Reset to reference")]
    public void ResetToReference()
    {
        // CopySerialized will overwrite the object name as well; we don't want that
        // so save it to a temporary variable so we can restore it afterwards
        var originalName = this.name;

        var reference = AssetDatabase.LoadAssetAtPath<CopySerializedExample>("Assets/ReferenceAsset.asset");
        EditorUtility.CopySerialized(reference, this);

        // Restore the original name
        this.name = originalName;
    }
    #endif
}
```
