Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clone/duplicate an existing GameObject and its children

Is there a C# way in Unity to duplicate an existing GameObject and all of its children? In my case, I have an empty GameObject with a number of Text objects as children and I would like to create a copy of them all, including relative positions, text values, font, colors, etc....

Prefabs won't work easily because I want to copy the object including its current state.

like image 307
Stephen Docy Avatar asked Jan 26 '18 23:01

Stephen Docy


1 Answers

The Instantiate function is used to clone any GameObject and its hierarchy.

public GameObject rootObj;

void Start()
{
    GameObject duplicate = Instantiate(rootObj);
}
like image 189
Programmer Avatar answered Sep 24 '22 07:09

Programmer