Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
3
Question by kdubb · May 22, 2011 at 07:01 PM · scriptableobjectcloneduplicatebehaviour

MonoBehaviours with ScriptableObjects and duplicating`

I have the following classes...

 abstract class Animation : ScriptableObject {
   abstract void Play();
 }
 
 class MoveAnimation : Animation {...}
 class RotateAnimation : Animation {...}
 
 class Button : MonoBehaviour {
 
   List< Animation > animations;
 
   void Update()
   {
     if(IsPressed()) {
       foreach(Animation anim : animations)
         anim.Play();
     }
 
   }
 }

This is all currently working properly; when I press the button it plays the animations. Also, I have built a custom editor that allows be to easily add animations to the button.

The problem arises when I duplicate a button in the editor (i.e. Ctrl+D). It creates a new Button but seems to copy not clone the list of animations. So the two seeming different button instances share one list of animations which causes headaches while editing.

The obvious solution is to clone the list of animations (and the object's inside) but I am not sure how/when to do this. So...

How/when do I handle the cloning of my internal objects when the owning MonoBehaviour has been cloned/duplicated?

Comment
Add comment · Show 9
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 oxium · Jul 01, 2011 at 05:36 AM 0
Share

hey did you find a solution to this problem ? (i'm actually having the same issue ! cheeers

avatar image eduwushu · Jul 20, 2011 at 11:44 AM 0
Share

When you delete elements from the list of animations... does everything goes well? I have a similar situation and deleting elements from the list and destroying the scriptableobject instance leads to a situation where the list finally has a null-like reference to an element and in inspector it is shown as a '$$anonymous$$isssing object'

avatar image moichezmoi · Mar 27, 2012 at 11:25 AM 0
Share

Hi,

Did one of you finally find a solution? I'm also having this problem...

Is there no way to override object.Instaniate(), or to catch the duplication event so that we could include our own code and do whatever we want to do in this case?

,

avatar image sebas77 · Mar 27, 2012 at 01:41 PM 0
Share

this does not solve your problem, but just for curiosity, why it must be a scriptableobject? Cannot it be just a normal c# class?

avatar image moichezmoi · Mar 27, 2012 at 02:11 PM 2
Share

Because of Unity serialization system, as explaind here.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Bunny83 · Mar 27, 2012 at 11:45 AM

The only point where you can hook into the event chain is Reset(). It's actually the "logical constructor". It is truly called only once, even for serialized objects. It is only called when an object get its first initialization. So in Reset() you could do something like:

 void Reset()
 {
     List<Animation> tmp = List<Animation>();
     if (animations != null && animations.Count > 0)
     {
         foreach(Animation A in animations)
         {
             tmp.Add((Animation)Instantiate(A));
         }
     }
     animations = tmp;
 }

Not tested but in theory it should work.

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 moichezmoi · Mar 27, 2012 at 12:46 PM 0
Share

Thanks for the answer. However, it seems that Reset() isn't called when duplicating a gameObject...

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

9 People are following this question.

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

Related Questions

How to reset a component on duplication? 4 Answers

restricting clone number 2 Answers

Placing Prefabs in Prefabs. 9 Answers

strange cloning array beviour 1 Answer

Deep Copy ScriptableObject 3 Answers


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