- Home /
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?
hey did you find a solution to this problem ? (i'm actually having the same issue ! cheeers
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'
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?
,
this does not solve your problem, but just for curiosity, why it must be a scriptableobject? Cannot it be just a normal c# class?
Because of Unity serialization system, as explaind here.
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.
Thanks for the answer. However, it seems that Reset() isn't called when duplicating a gameObject...
Your answer
Follow this Question
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