- Home /
How to clone or re-parent a Component?
I'd like to re-parent a Component, or to clone an existing Component and attach it to some other GameObject.
I have tried GetComponent<T>().transform.parent = myNewParent.transform
but this will not work because the documentation says transform
is the parent GameObject's transform.
Looking at GameObject.AddComponent(...)
, I can only pass in a type, not an existing instance of a Component.
Is it possible to re-parent a Component and if so how?
Answer by BerggreenDK · Jul 04, 2011 at 12:37 AM
I dont think you need to move it.
If you know what Component you want. You can just ADD it to the GameObject you want. If you need the values equal, you can make a simple script that copies those after adding the GameObject.
The GameObject
http://unity3d.com/support/documentation/ScriptReference/GameObject.html
Add Component
http://unity3d.com/support/documentation/ScriptReference/GameObject.AddComponent.html
Answer by Bunny83 · Jul 04, 2011 at 12:47 AM
Short answer is: No!
The owner of a component can't be changed.
Answer by team_eden · Dec 27, 2013 at 07:55 PM
Alternatively you can turn off the object's mesh renderer, and collider, until the particle cycle finishes; then, destroy the object. -TE
Answer by NeatWolf · Dec 26, 2016 at 07:14 PM
After 3 years, is there any other viable solution other than reinstancing all components on the target GameObject and restoring their values?
Your answer
![](https://koobas.hobune.stream/wayback/20220612132142im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Find Script in parent Gameobject? 3 Answers
Parent class cannot find component 0 Answers
Make a simple tree 1 Answer
How can I determine the parent of a component? 2 Answers
Can you set a component (audio source) position manually 2 Answers