Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.
CloseFor some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.
Closesource | The object to copy the properties from. |
dest | The object to copy the properties to. |
Copies all serialized properties of a Unity Object from one object to another.
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.
This method is mostly useful when working with objects in assets. It is similar to Object.Instantiate 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 are not copied.
If the object being copied derives from MonoBehaviour or ScriptableObject, 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 method is implemented then it's invoked on the source
object before copying. If the ISerializationCallbackReceiver.OnAfterDeserialize 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.
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 }
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.