• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by WILEz1975 · Feb 22, 2015 at 03:00 PM · components

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[i]!=null&&components[i]!=this){
                     
                     CompType=components[i].GetType();
                     TargetGameObjec.AddComponent<CompType>();
     
                     print (CompType);
                 }
     
                 if(components[i]==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?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image RudyTheDev · Feb 22, 2015 at 04:02 PM 0
Share

Search for "unity component copy", this has been answered. You will need reflection at run-time.

5 Replies

· Add your reply
  • Sort: 
avatar image
-1

Answer by Markus_T · Dec 14, 2016 at 08:28 PM

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);
             }
         }
     }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image LaDungDev · May 21, 2018 at 08:26 AM 0
Share

Thanks you!

avatar image
6

Answer by jnt · Aug 24, 2016 at 11:30 AM

As per another thread: http://answers.unity3d.com/questions/12653/editor-wizard-copy-existing-components-to-another.html

You can use:

 var components = GetComponents<Component>();

Then loop through those components and do:

 UnityEditorInternal.ComponentUtility.CopyComponent(components[i]);
 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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image LaDungDev · May 21, 2018 at 08:25 AM 0
Share

Thanks you so much! :D

avatar image
0

Answer by Wanzyee · Jan 08, 2016 at 09:04 AM

  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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by WILEz1975 · Feb 22, 2015 at 10:42 PM

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!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by DiegoSLTS · Feb 22, 2015 at 03:59 PM

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 (http://docs.unity3d.com/ScriptReference/GameObject.AddComponent.html), 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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image RudyTheDev · Feb 22, 2015 at 04:04 PM 0
Share

To clarify: that will only add new components to the target object. It won't copy any of the data. It will also call Awake()/Start(), which may mess it up under certain scenarios.

Also TargetGameObjec.AddComponent(CompType); should also work, as AddComponent can accept Type.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

24 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to add a component to all selected objects? 1 Answer

Import assets, linking their components automaticly. 0 Answers

Destroying Components from Editor Script Throwing MissingReferenceException 1 Answer

Model - get mesh component 0 Answers

Character controller not fitting through door 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges