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 Fallout · May 15, 2012 at 10:10 AM · c#gameobjecteditor

Best practice for copying Components from one GameObject to another?

Hello fellow Unity users. Im writing an Editor Script that should copy all Components from one GameObject to another. Unfortunately I have encountered some problems in doing so.

My first atempt was using the EditorUtility.CopySerialized function:

 Foo from = gameObjectFrom.GetComponent<Foo>();
 Foo to = gameObjectTo.AddComponentn<Foo>();
 EditorUtility.CopySerialized(from, to);

CopySerialized seems to be working. Unfortunatelly later I get the Error 'Component (Foo) has a broken GameObject reference. Fixing!'

Then I tried using Reflection:

 foreach(FieldInfo field in fromComponent.GetType().GetFields())
     field.SetValue(toComponent, field.GetValue(fromComponent));

When I try using this on an Animation Component, it has no fields. Instead the Animation Component has 31 Properties and Im not sure if it's a good idea to reflection-set all of these properties.

So, is there a good and reliable way to copy Components from one GameObject to another and that will work with all 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

3 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by Eran-Yaacobi · Nov 10, 2013 at 09:59 PM

EditorUtility.CopySerialized should work, but if it doesn't, you can simply use the (undocumented) UnityEditorInternal.ComponentUtility class:

 UnityEditorInternal.ComponentUtility.CopyComponent(copiedComponent);
 UnityEditorInternal.ComponentUtility.PasteComponentAsNew(targetGameObject);

This seems to be identical to the actual commands in the component's context-menu in the inspector, so there shouldn't be any problems with it.

Comment
Add comment · Show 2 · 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 Meceka · Apr 14, 2014 at 02:12 PM 0
Share

This comment is for people who is wishing to build a game with this code. (I didn't understand that this question was based on editor only).

I have to type "Using UnityEditorInternal" in beginning of script. And unity can't build game giving error about unityeditorinternal not being available.

avatar image Eran-Yaacobi · May 08, 2014 at 10:16 PM 0
Share

This is editor code, and as such, you need to surround it with:

 #if UNITY_EDITOR  
     // Editor code here.
 #endif

This causes the compiler to ignore the content in between when building the game.

$$anonymous$$oreover, as you've mentioned, you do need to add a 'using' statement, though it should be "using UnityEditor" - there is no need to specify "UnityEditorInternal" directly. Again, UnityEditor is not available when building a game, so you need to surround the 'using' clause with the '#if UNITY_EDITOR' clause as well.

However, all this is irrelevant if the code is in an editor script (scripts under an "Editor" folder), as editor scripts are not used when building the game, and as they should contain "using UnityEditor" anyhow.

avatar image
1

Answer by Bunny83 · May 15, 2012 at 10:14 AM

Unity uses it's own serializer. Public supported fields as well as private fields (marked with SerializeField) are serialized and saved. You could try to roll your own mechanism to find all the variables that should be serialized, or just use EditorUtility.CopySerialized which is made for this purpose ;)

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 Fallout · May 15, 2012 at 10:20 AM 0
Share

If I use CopySerialized on an Animation Component I later get the 'broken GameObject reference' error as I mentioned earlier.

avatar image
0

Answer by Nition · Sep 07, 2015 at 10:18 PM

I know this was asked years ago but I'm a bit surprised that no-ones ever actually given the most likely answer.

This may have changed now in Unity 5, but it Unity 4, when using logarithmic rolloff mode, the .maxDistance setting of an AudioSource doesn't actually determine when the sound volume reaches zero - it actually sets the point where it stops attenuating.

So unlike a linear rolloff, the smaller your maxDistance value is set, the louder your sound will be when far away.

You can fix this by adjusting the curve yourself to go to zero at or just before the maxDistance point. Unfortuntately there's currently no way to set a custom curve in script.

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 getyour411 · Sep 07, 2015 at 11:01 PM 2
Share

@Nition this looks to have been posted to the wrong question.

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

8 People are following this question.

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

Related Questions

Custom Inspector: Targets & GameObjects 1 Answer

C# Rotate GameObjects Regardless of List Size 2 Answers

How to get a variable value from another script(C#)? 1 Answer

C# GetComponent Issue 2 Answers

How do I force a GameObject with children to collapse and expand in the hierarchy view? 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