- Home /
Is it possible to duplicate an Item that is proceduraly generated with random elements?
I have an empty with a script on it that instantiates a bunch of cubes in random positions in the start function. I'd like to make exact copies of the empty after the game has been started but when I try to duplicate the original empty, using Instantiate(), the cubes are all re instantiated in new positions. This behavior is not exactly surprising, but I can't figure out how to make exact copies of the original object. Is it possible?
$$anonymous$$eep references to the instantiated cubes in an array or list, and use those references to make clones at the same positions.
If you're trying to make the script generate the same thing, use the same seed for the random. If you want to duplicate it, maybe try disabling the generation script.
Answer by Glurth · Feb 11, 2016 at 04:36 PM
To expand on Cherno's comment, though I'm not quite sure if this is the answer you are looking for:
Lets say your class contains some cubes: GameObject[] cubes;
If you simply copy this array into your new object, you will be referencing the same cubes that the original references. To create COPIES of all those cubes, You will need to loop through each cube, and create a new instance:
newcubearray[i] = GameObject.Instantiate(cubes[i]);
Important note: When you instantiate a game object, you will not only create a new copy of that object in the heirarchy, you will ALSO, create copy of all it's children. Perhaps this is where those unexpected cube positions are coming from? (if so, you can probably resolve this by simply making the cubes NOT the children of the object, or alternatively, perform a copy manually: instantiate an empty game object, and add/initialize components as needed)
Your answer
![](https://koobas.hobune.stream/wayback/20220612063750im_/https://answers.unity.com/themes/thub/images/avi.jpg)