Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
1
Question by ABD · Jul 03, 2014 at 10:46 AM · c#gameobjectmovenothing

Can I move component of one gameobject to other.

Can I move component of one gameobject to other gameobject using c#.

Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
4

Answer by Cherno · Nov 28, 2014 at 03:27 PM

This will copy the values from one component to another of the same type. Substitute YourComponentType with the type of component which needs to be copied.

 public CopyClassValues(YourCompomentType sourceComp, YourCompomentType targetComp) {
      FieldInfo[] sourceFields = sourceComp.GetType().GetFields(BindingFlags.Public | 
                                                       BindingFlags.NonPublic | 
                                                       BindingFlags.Instance);
      int i = 0;
      for(i = 0; i < sourceFields.Length; i++) {
           var value = sourceFields[i].GetValue(sourceComp);
       sourceFields[i].SetValue(targetComp, value);
      }
 }
Comment
Add comment · Show 3 · 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 _dns_ · Nov 28, 2014 at 03:51 PM 0
Share

Hi, if what you want is done in the Editor and not in the standalone game, you can also use : http://docs.unity3d.com/ScriptReference/EditorUtility.CopySerialized.html

avatar image bateleur · Feb 03, 2017 at 10:18 AM 3
Share

Note that this solution is a workaround for a few common cases, but doesn't work in general. The problem is that any references to the original object (sourceComp) from elsewhere will not automatically update to point to the new copy. As such, this process does not function like a "move".

avatar image dalirmc · Aug 29, 2019 at 10:11 PM 0
Share

It worked! Thanks

avatar image
0

Answer by YoungDeveloper · Jul 03, 2014 at 10:49 AM

Sure you can, getcomponent and addcomponent, cache the existing component, add it on other GO, then remove it.

http://docs.unity3d.com/ScriptReference/GameObject.GetComponent.html

http://docs.unity3d.com/ScriptReference/GameObject.AddComponent.html

Comment
Add comment · Show 3 · 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 Tigran · Nov 28, 2014 at 02:34 PM 3
Share

This will create a new component and add that to the gameobject, it won't copy all the values of the previous component.

avatar image Xitech_ · Nov 28, 2014 at 02:38 PM 0
Share

You are wrong @Tigran "cache the existing component, add it on other GameObject"

avatar image Tigran · Nov 28, 2014 at 02:48 PM 3
Share

@levoTNTO Take a look at the AddComponent function, you only specify a type of the component, you can't pass the object to be copied.

avatar image
0

Answer by sonnyb · Nov 08, 2018 at 04:56 PM

No, there is no way to move a component to a different GameObject.

There are workarounds (as explained by other answers) that are likely sufficient for many cases, but there are some limitations.

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

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.

Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

Gameobjects move towards a moving object c# 1 Answer

Moving GameObject to various position ? 1 Answer

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

Instantiated GameObject Not Rendering 1 Answer


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