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 rtaylor205 · Mar 14, 2015 at 02:12 AM · componentmonobehaviour

Adding components from prefab into existing GameObject

I've built a MonoBehaviour that has a fields that are used to control the movement of the GameObject it is attached to.

I'm looking for a solution that allows my artists to come up with a bunch of pre-determined setups of this component. Storing that either as prefabs, or in some other library(?). Then a way to add that version of the component, into an existing GameObject from C# anywhere during game-play.

I'm reasonably new to Unity. To me, option 1 sounds terrible. Option 2 sounds like it's not too complex, but it doesn't seem right to me. I'm hoping there's just some feature I haven't come across yet.

  1. Create a bunch of prefabs ("Mover A", "Mover B", "Mover C"), and store those in /Resources/. At play time, instantiate these prefabs, copy the component from the new instance into the existing one, destroy the new instance.

  2. Change my MonoBehaviour so that it actually controls the movement of its own parent. Then instantiating the prefabs, adding them as children of the existing GameObject.

  3. Some other, elegant solution I don't know about.

Thanks!

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 maccabbe · Mar 14, 2015 at 02:21 AM 1
Share

Option 1 is impossible, you cannot change the owner of a component.

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Cherno · Mar 14, 2015 at 02:55 AM

Should be pretty easy. You don't even have to instantiate a prefab, as long as you have it stored in a variable you can access it's scripts. The only thing you have to be very careful with is using the component data from the prefab to fill in those on the gameObject. You have to make sure that you copy all component values, and not just create references, because this might irreversibly change the values on your original prefab's components. There are various ways of making "deep copies" of classes, here is one example.

 public YourComponentType CopyYourComponentTypeValues(YourComponentType source) {
         
         YourComponentType target = new YourComponentType();
         
         FieldInfo[] sourceFields = source.GetType().GetFields(BindingFlags.Public | 
                                                               BindingFlags.NonPublic | 
                                                               BindingFlags.Instance);
         
         
         int i = 0;
         
         for(i = 0; i < sourceFields.Length; i++) {
             var value = sourceFields[i].GetValue(source);
             sourceFields[i].SetValue(target, value);
         }
         
         return target;
     }

Ther are also methoeds do it it in a more general way without having to pass and return specific types; This however goes beyond the scape of this post so I recommend checking over at StackOverflow or similar for such functions.

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
1

Answer by Wanzyee · Mar 04, 2016 at 09:40 AM

You can take a look at Component Clipboard.

Although this looks like an Ad, the reason I made this is similar to you. It's artist friendly, copy and paste components just by drag and drop. And there's also a free version, use it with context menu.

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 Bunny83 · Mar 04, 2016 at 01:38 PM 1
Share

Have you read this:

Then a way to add that version of the component, into an existing GameObject from C# anywhere during game-play.

Your solution seems to be a pure editor feature which Unity supports now out of the box (though, not with drag & drop support). Up to now there's no built-in way to copy just a component at runtime. You would need to do a deep copy manually as Cherno explained in his answer. From your asset description it's not clear if you have any support for runtime component copy operations.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Change MonoBehaviour component name in prefab text file? 0 Answers

When do components of loaded prefabs get initialized? 1 Answer

How to Use Components and Interfaces together? 0 Answers

How to change Component's (MonoBehaviour Script) header (name) shown in Inspector from Custom Editor? 0 Answers

Multiple MonoBehaviours of the same type on single object ? 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