Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
0
Question by PsychoDuckArcade · Oct 09, 2021 at 11:59 PM · componentmovecopy

How to copy components and move components between gameobjects in unity?

I know this is probably a nooby question but I can't figure out how to simply copy an instance of a component or how to attach/reattach component instances to different game objects. Everything I find online is either editor only or involves reflection which I'd like to avoid if i can for performance.

I thought AddComponent would take component instances as an argument like instantiate(gameobject) does but it doesnt seem to have that..

What's the intended unity way of copying and moving components?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Bunny83 · Oct 10, 2021 at 12:58 AM

There simply is no way at the moment. Component instances are tied to their housing gameobject. Components can not be detached and attached to other gameobjects at all. So your only solution is to create the same component on your target gameobject and copy all values over and destroy the old one. When the component itself provides this functionality, you don't need reflection. However if you want to support any kind of component, reflection is the only real solution at the moment.


Though keep in mind that even reflection has its limits. All build-in components are actually implemented in native C++ code and you only have a managed wrapper class with properties that map to native methods. So those components do not have managed fields that could be copied, only properties.


You can copy whole gameobjects including its components but not individual components on their own. Think of it like you're being able to clone a human but you can not detach an arm of one human and stitch it onto another. This would require more advanced surgery.


Technically every component has a managed and a native part. In case of monobehaviours the native part of the component if of course always the same C++ class and the actual behaviour of the class is implemented in Mono / .NET (hence the name since from the engine's perspective it's a behaviour that is delegated to the managed side). Theoretically it should be possible to detach components and move them to other gameobjects. However this could have countless of issues when you think about the Start method. If you suddenly change the host gameobject, all references gathered in Start would be pointing to the old hosting gameobject. That's why Unity does not even support such an action.


Technically on the native side a MonoBehaviour component and a ScriptableObject are the same thing, just that a ScriptableObject is not attached to a gameobject. Both MonoBehaviour and ScriptableObject are represented in native code by the same class. At least that got confirmed by Richard Fine in his ScriptableObject talk (there are actually two versions, here's the other with basically the same content.).

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

127 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

[HOW] Copy and Transfer Component with Values to other object? 1 Answer

Copy / Paste As New not copying deep 1 Answer

Copying a Component's values without a new Game Object 1 Answer

What happens when i Copy an object (From Programming point of view) 1 Answer

Copy Transform 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