Copy all components from a GameObject and paste to another GameObject

using UnityEngine;
using System.Collections;
using System;
public class CopyComponents : MonoBehaviour {

    	Component[]components;
    
    	public GameObject SourceGameObject;
    	public GameObject TargetGameObjec;
    	static Type CompType;
    
    	void Start () {
    		if(SourceGameObject==null)SourceGameObject=gameObject;
    
    		components=SourceGameObject.GetComponents<Component>();
    		for (int i = 0; i < components.Length; ++i) {
    			if(components_!=null&&components*!=this){*_

_ CompType=components*.GetType();
TargetGameObjec.AddComponent();*_

* print (CompType);*
* }*

_ if(components*==null)
Debug.LogWarning(“Warning, component n°”+i+“not found in this project”);
}*_

* }*

* void Update () {*

* }*
}

This is my script.
Basically, as the title, I want copy all components (or Behaviour) and paste to another GameObject.
But with my code return this error:
Assets/CopyComponents.cs(20,62): error CS0118: CopyComponents.CompType' is a field’ but a `type’ was expected
Anyone can help me?

As per another thread: Editor Wizard: Copy Existing Components to Another GameObject - Unity Answers

You can use:

var components = GetComponents<Component>();

Then loop through those components and do:

UnityEditorInternal.ComponentUtility.CopyComponent(components*);*

UnityEditorInternal.ComponentUtility.PasteComponentAsNew(targetGameObject);
But that’s obviously not at runtime, so not sure if that answers the question in this case, but very useful as a tool in the editor.

The compiler expects an actual Class name for that line, not a variable. When you use generics they’re resolved at compile time.

According to the documentation (Unity - Scripting API: GameObject.AddComponent), you can use this form of AddComponent instead:

TargetGameObjec.AddComponent(className);

Instead of setting a type at compile time, pass a type’s name as a parameter in runtime. I’m not sure if you need to pass CompType.ToString() or CompType.Name, but probably both work.

ok I solve the copy of components… But are not a real copy. I want the same state, with all same variables for all components… And reflection not work, my field array is always empty!

  1. You can code “AddComponent< Type >” directly without variable.

  2. Or, pass the type variable to “AddComponent(CompType)” without generic “< T >”.

  3. Yep, you have to copy members, not only add component. See here.

  4. Welcome to take a look at my Component Clipboard, a convenient utility to copy multiple components. But it currently can’t copy whole GameObject at once.

  5. This question might duplicate with another. And there were answers.

Here is a method that copies all the components from one gameobject to another. I didn’t include the Transform, MeshFilter or MeshRenderer in the copy but that should be easy to edit.

    private void CopySpecialComponents(GameObject _sourceGO, GameObject _targetGO)
    {
        foreach (var component in _sourceGO.GetComponents<Component>())
        {
            var componentType = component.GetType();
            if (componentType != typeof(Transform) &&
                componentType != typeof(MeshFilter) &&
                componentType != typeof(MeshRenderer)
                )
            {
                Debug.Log("Found a component of type " + component.GetType());
                UnityEditorInternal.ComponentUtility.CopyComponent(component);
                UnityEditorInternal.ComponentUtility.PasteComponentAsNew(_targetGO);
                Debug.Log( "Copied " + component.GetType() + " from " + _sourceGO.name + " to " + _targetGO.name);
            }
        }
    }