• 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 buzz.burrowes · Jul 25, 2015 at 03:57 PM · componentinstancecopypaste

Copy / Paste As New not copying deep

I've got this class...

   [System.Serializable]   
   public class LoopConfig 
   {
      public AudioClip Clip = null;
      public AnimationCurve Envelope = new AnimationCurve();
      public float Gain = 1.0f;
   }

I've got this other class that contains one of those...

    public class CrossfadedLoopConfig : ScriptableObject
    {
       public LoopConfig LoopSettings = null;
       public AnimationCurve Fader = new AnimationCurve();
    }

I;ve got this MonoBehaviour...

    public class LoopCrossfader : MonoBehaviour
    {
       public float Gain = 1.0f;
       public float XFadePosition = 0.0f;
       
       public List<CrossfadedLoopConfig> LooperSettings = new List<CrossfadedLoopConfig>();
 
       void Start()
       {
          ... 
       }
 
       blah, blah, blah
    }

I've got a custom editor for this component, and it works great. Here is my problem...

If I right click on the component's name and select "Copy Component", and then right click again on the component's name and select "Past Component as New"... I don't get a deep copy.

The new (pasted) component's list of looper settings "points to" the original component's looper settings. So, here is an example...

If I have an instance of this component with 2 items in the LooperSettings list, and I copy and paste, and then I edit the first item in the original component instance's list of looper settings, I see those changes in the pasted component instance as well.

So... is there any way for me to get the paste to do a deep copy?

Thanks, Buzz

Comment
Add comment · Show 2
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 HypothicEntropy · Oct 29, 2015 at 03:52 PM 0
Share

I am currently trying to figure out the solution to this problem as well. Changing the values of a copied/pasted component changes the values on the other component which means they are pointing to the same variables rather than creating new one - which you would expect it would do when using 'paste component as new'.

avatar image callen · Jan 08, 2016 at 06:52 PM 0
Share

Would it make sense for you to convert the CrossfadedLoopConfig class to $$anonymous$$onoBehavior, and attach it to the same object which has the LoopCrossfader? If you did this, duplicating the game object ins$$anonymous$$d of the component will give you duplicates of each config component.

What you're really asking is why won't Unity create new CrossfadedLoopConfigs for you when you paste the component values. The reason is because the values aren't "part of" the LoopCrossfader component, only the references are (and those are what gets copied).

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Wanzyee · Jan 08, 2016 at 06:04 PM

Maybe, you can create a new list from the original one in script.

 void Awake(){
     LooperSettings = new List<CrossfadedLoopConfig>(LooperSettings);
 }

Try to do that when Awake. it's called after the new MonoBehaviour being instantiated, just make sure the GameObject active. Or, implement ISerializationCallbackReceiver with OnAfterDeserialize.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

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

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

Updating variable on another script. It is not working. 2 Answers

How do I keep a texture different on copied objects? 1 Answer

Stopping Copy/paste into Textfield. 3 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges