- Home /
Duplicate Monobehaviour
I am trying to duplicate one Monobehaviour script onto another gameObject. How would I go about doing this in code?
Basically, I have a bunch of dynamic post effects on the camera I would like to duplicate onto another gameObject. I won't necessarily know what specific post effects will be on the camera.
Why can't you just save the camera in a prefab and Instantiate
it again?
That is a work around that won't quite work and this will require an explanation of what I am doing. Basically, I am making an asset bundle for a level which include light probes, lightmapping (all of that is done), and camera post effects. Now, turning the camera into a prefab and reinstantiating it would work to save the post effects normally, but how would I then get the post effects off of the camera and onto the scene camera (that camera is even more complicated)? At one point in the pipeline, the effects need to be moved over one way or the other.
Answer by Ray-Pendergraph · Jun 29, 2013 at 02:43 PM
You cannot just put a MonoBehaviour instance that was created on one game object onto another. Probably your best bet is to write a Copy(MyController anotherController) method and copy all the attributes that would make it (for your purposes) identical. It will not however have the same unique identifier and will not be the same object. If you need == or .Equals to work you can override these to make it act like the same based on your important attributes. If you want to do that check out this link
Your answer
Follow this Question
Related Questions
What process should I best use to properly create an updated copy of a Prefab? 1 Answer
How to have a cloned/duplicated enemy react the same way the original does? 1 Answer
Problem enemy ragdoll duplicate. 0 Answers
Duplication(instance) re-create deleted children 0 Answers
How to make duplicate unique? 1 Answer