Search Unity

How to Copy Component Value from an Object to Another

Discussion in 'Scripting' started by AHMADF000, Jun 19, 2019.

  1. AHMADF000

    AHMADF000

    Joined:
    Oct 17, 2017
    Posts:
    36
    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?
     
  2. EdGunther

    EdGunther

    Joined:
    Jun 25, 2018
    Posts:
    183
    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
     
  3. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    You can "paste component as new", but i think hes asking about doing it in runtime with scripts.
     
    Takiden likes this.
  4. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,443
    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.
     
    SparrowGS likes this.
  5. Ian094

    Ian094

    Joined:
    Jun 20, 2013
    Posts:
    1,548
  6. AHMADF000

    AHMADF000

    Joined:
    Oct 17, 2017
    Posts:
    36
    Yes, that's what i meant, i need to swap object property in runtime but i can't use setActive or Instantiate +Destroy
     
    Last edited: Jun 20, 2019
  7. AHMADF000

    AHMADF000

    Joined:
    Oct 17, 2017
    Posts:
    36
    Thanks! i'll look into both method, and see what works for me.
     
  8. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    You can use JSONUtility for this:

    Code (csharp):
    1. void CopyValues(MyComponent from, MyComponent to) {
    2.     var json = JsonUtility.ToJson(from);
    3.     JsonUtility.FromJsonOverwrite(json, to);
    4. }
    5.  
    6. // generic version:
    7.  
    8. void CopyValues<T>(T from, T to) {
    9.     var json = JsonUtility.ToJson(from);
    10.     JsonUtility.FromJsonOverwrite(json, to);
    11. }
    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.
     
  9. AHMADF000

    AHMADF000

    Joined:
    Oct 17, 2017
    Posts:
    36
    Yeah i think i need to do this manually
    Thanks for the suggestion!
     
  10. Mariusz-Born7

    Mariusz-Born7

    Joined:
    Sep 4, 2015
    Posts:
    40
    You can use EditorUtility.CopySerializedManagedFieldsOnly or EditorUtility.CopySerialized.

    Code (CSharp):
    1. using UnityEditor;
    2. using UnityEngine;
    3.  
    4. public class CopyComponentWindow : EditorWindow
    5. {
    6.     public Component srcComponent;
    7.     public Component dstComponent;
    8.  
    9.     [MenuItem("Window/My Tools/Copy Component")]
    10.     public static void OpenWindow()
    11.     {
    12.         CopyComponentWindow wnd = GetWindow<CopyComponentWindow>("Copy Component");
    13.     }
    14.  
    15.     public void OnGUI()
    16.     {
    17.         srcComponent = (Component)EditorGUILayout.ObjectField("From", srcComponent, typeof(Component), true);
    18.         dstComponent = (Component)EditorGUILayout.ObjectField("To", dstComponent, typeof(Component), true);
    19.  
    20.         if (GUILayout.Button("Copy Values"))
    21.         {
    22.             if (srcComponent != null && dstComponent != null && srcComponent != dstComponent)
    23.             {
    24.                 Undo.RegisterCompleteObjectUndo(dstComponent, "Component");
    25.                 EditorUtility.CopySerializedManagedFieldsOnly(srcComponent, dstComponent);
    26.             }
    27.         }
    28.     }
    29. }
     
    marked_one, hamza_unity995 and GazaG like this.
  11. TiggyFairy

    TiggyFairy

    Joined:
    Dec 22, 2019
    Posts:
    506
    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...from-object-a-to-the-same-component-on-object

    You only need the first block from the first answer.
     
    Last edited: Feb 22, 2023
    Draxidious and Bunny83 like this.