How to Copy Component Value from an Object to Another

If i have 2 object with the same component and childs (the childs also have same component) can i copy component from 1 object to other?

So theres this gear with a down arrow on the top right of any component. Click that, then click copy component. This will copy the component values.

Then go to another gameobject, add the same type of component, then right click and paste component values.

I’m not sure if you can actually paste a whole new component. You can ctr+d a gameobject to clone it if thats easier.

Cheers

You can “paste component as new”, but i think hes asking about doing it in runtime with scripts.

1 Like

The “paste values” functionality should be just a case of serializing data into a component, but Unity decided not to make MonoBehaviour directly serializeable. Instead you’d have to do some reflection and serialize all the serializable fields individually. Fun stuff.

1 Like

You can also use presets:

1 Like

Yes, that’s what i meant, i need to swap object property in runtime but i can’t use setActive or Instantiate +Destroy

Thanks! i’ll look into both method, and see what works for me.

You can use JSONUtility for this:

void CopyValues(MyComponent from, MyComponent to) {
    var json = JsonUtility.ToJson(from);
    JsonUtility.FromJsonOverwrite(json, to);
}

// generic version:

void CopyValues<T>(T from, T to) {
    var json = JsonUtility.ToJson(from);
    JsonUtility.FromJsonOverwrite(json, to);
}

Problem is that it’s both a bit slow and very gc-heavy, so if you’re doing this a lot, you’ll probably want to just do it manually.

5 Likes

Yeah i think i need to do this manually
Thanks for the suggestion!

You can use EditorUtility.CopySerializedManagedFieldsOnly or EditorUtility.CopySerialized.

using UnityEditor;
using UnityEngine;

public class CopyComponentWindow : EditorWindow
{
    public Component srcComponent;
    public Component dstComponent;

    [MenuItem("Window/My Tools/Copy Component")]
    public static void OpenWindow()
    {
        CopyComponentWindow wnd = GetWindow<CopyComponentWindow>("Copy Component");
    }

    public void OnGUI()
    {
        srcComponent = (Component)EditorGUILayout.ObjectField("From", srcComponent, typeof(Component), true);
        dstComponent = (Component)EditorGUILayout.ObjectField("To", dstComponent, typeof(Component), true);

        if (GUILayout.Button("Copy Values"))
        {
            if (srcComponent != null && dstComponent != null && srcComponent != dstComponent)
            {
                Undo.RegisterCompleteObjectUndo(dstComponent, "Component");
                EditorUtility.CopySerializedManagedFieldsOnly(srcComponent, dstComponent);
            }
        }
    }
}
3 Likes

Sorry to resurrect this, but that won’t work in a release version as it uses editor code. Editor code only works in the editor. Just found this by googling, and I don’t want others to be mislead. Try this code: https://stackoverflow.com/questions/62553142/how-to-copy-values-of-a-component-from-object-a-to-the-same-component-on-object

You only need the first block from the first answer.

2 Likes