Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Resolved How can I copy component values to the same component in another object in C#

Discussion in 'Editor & General Support' started by LuizDusky, Oct 26, 2022.

  1. LuizDusky

    LuizDusky

    Joined:
    Jul 12, 2018
    Posts:
    4
    I'm trying to create a tool that takes care of copying the components or the values if the object already has that component. I can copy the components correctly, but I can't copy the component values because I don't know how to compare if the destination has the same components and which ones. So I tried another approach where I use two separate functions for each method, but unity gives me the error "cannot convert from "UnityEngine.GameObject" to "UnityEngine.Component"". because I need to paste the values in the component itself and not in the object, I believe, but how? here is my code:
    Code (CSharp):
    1.     public void CopySpecialComponents()
    2.     {
    3.         foreach (GameObject go in targets)
    4.         {
    5.             foreach (var component in source.GetComponents<Component>())
    6.             {
    7.                 var componentType = component.GetType();
    8.                 if (componentType != typeof(Transform) &&
    9.                     componentType != typeof(MeshFilter) &&
    10.                     componentType != typeof(MeshRenderer)
    11.                     )
    12.                 {
    13.                     Debug.Log("Found a component of type " + component.GetType());
    14.                     UnityEditorInternal.ComponentUtility.CopyComponent(component);
    15.  
    16.                     UnityEditorInternal.ComponentUtility.PasteComponentAsNew(go);
    17.  
    18.  
    19.                     Debug.Log("Copied " + component.GetType() + " from " + source.name + " to " + go.name);
    20.                 }
    21.             }
    22.         }
    23.     }
    24.     public void CopySpecialValues()
    25.     {
    26.         foreach (GameObject go in targets)
    27.         {
    28.             foreach (var component in source.GetComponents<Component>())
    29.             {
    30.                 var componentType = component.GetType();
    31.                 if (componentType != typeof(Transform) &&
    32.                     componentType != typeof(MeshFilter) &&
    33.                     componentType != typeof(MeshRenderer)
    34.                     )
    35.                 {
    36.                     Debug.Log("Found a component of type " + component.GetType());
    37.                     UnityEditorInternal.ComponentUtility.CopyComponent(component);
    38.  
    39.                     UnityEditorInternal.ComponentUtility.PasteComponentValues(go);
    40.  
    41.  
    42.                     Debug.Log("Copied " + component.GetType() + " from " + source.name + " to " + go.name);
    43.                 }
    44.             }
    45.         }
    46.     }
     
  2. LuizDusky

    LuizDusky

    Joined:
    Jul 12, 2018
    Posts:
    4
    So I found that I was missing the convertion from var to type and the solution was simple as that!
    Here is how the code ended up like
    Code (CSharp):
    1.     public void CopySpecialComponents()
    2.     {
    3.         foreach (GameObject go in targets)
    4.         {
    5.             foreach (var component in source.GetComponents<Component>())
    6.             {
    7.                 var componentType = component.GetType();
    8.                 if (componentType != typeof(Transform) &&
    9.                     componentType != typeof(MeshFilter) &&
    10.                     componentType != typeof(MeshRenderer)
    11.                     )
    12.                 {
    13.                     Debug.Log("Found a component of type " + component.GetType());
    14.                     UnityEditorInternal.ComponentUtility.CopyComponent(component);
    15.  
    16.                     UnityEditorInternal.ComponentUtility.PasteComponentAsNew(go);
    17.  
    18.  
    19.                     Debug.Log("Copied " + component.GetType() + " from " + source.name + " to " + go.name);
    20.                 }
    21.             }
    22.         }
    23.     }
    24.     public void CopySpecialValues()
    25.     {
    26.         foreach (GameObject go in targets)
    27.         {
    28.             foreach (var component in source.GetComponents<Component>())
    29.             {
    30.                 var componentType = component.GetType();
    31.                 if (componentType != typeof(Transform) &&
    32.                     componentType != typeof(MeshFilter) &&
    33.                     componentType != typeof(MeshRenderer)
    34.                     )
    35.                 {
    36.                     Debug.Log("Found a component of type " + component.GetType());
    37.                     UnityEditorInternal.ComponentUtility.CopyComponent(component);
    38.                     foreach (var tarComponent in go.GetComponents<Component>())
    39.                     {
    40.                         if (tarComponent.GetType() == component.GetType())
    41.                         {
    42.                             Type typ = tarComponent.GetType();
    43.                             UnityEditorInternal.ComponentUtility.PasteComponentValues(go.GetComponent(typ));
    44.                             Debug.Log("Copied " + component.GetType() + " from " + source.name + " to " + go.name);
    45.                         }
    46.                     }
    47.                 }
    48.             }
    49.         }
    50.     }