- Home /
Instantiate a copy without its children
I want to copy an existing gameobject from my scene without copying its children. I can of course copy the complete gameobject (including its children) and delete the children afterwards but this leads to problems elsewhere in my code as the children are of a type that should not be destroyed. Is there a way I can copy/instantiate the parent without its children?
A possible solution might be instantiating a new object and making it similar to the original parent, however I'm not sure how to do this as the original parent has some specific components that needs to be on the newly instantiated object and I'm not sure how to do this.,I want to copy an existing gameobject from my scene without copying its children. I can of course copy the complete gameobject (including its children) and delete the children afterwards but this leads to problems elsewhere in my code as the children are of a type that should not be destroyed. Is there a way I can copy/instantiate the parent without its children.
A possible solution might be instantiating a new object and making it similar to the original parent, however I'm not sure how to do this as the original parent has some specific components that needs to be on the newly instantiated object and I'm not sure how to do this.
How are you getting the existing gameobject with children? If you're using a prefab, you could have one prefab for the parent and additional prefabs for the children. So when you want the full parent/children object you instantiate the parent and then the children but if you just want the parent, you can instantiate the parent on its own.
If this approach won't work in your scenario, you could try moving the children to a different parent when you copy, and them moving them back after the copy has been created. Not the most elegant solution but it should work.
Hmm no its not a prefab, but the moving thing might actually do the trick. Indeed it's not the most elegant solution so I'll see if someone comes up with something cleaner but otherwise I'll go with that approach. Thanks!
Your answer
Follow this Question
Related Questions
Create Clones as children in loops? 1 Answer
How to Access Components in Children of GameObject? 1 Answer
Instantiate as child 3 Answers
How to copy a Script as a child of another object 1 Answer
Destroying childs and Instantiate [C#] 0 Answers