- Home /
The question is answered, right answer was accepted
How to instantiate deep copy of a prefab
So I've been working on a infinite terrain generation system using chunks of terrain being loaded around the player (think minecraft) and I've run into a problem, when I instantiate a prefab or object it does make a full copy of it instead it holds a reference, what this means is that when I copy terrain it shares a height map, If I modify one of the terrain objects then all of them are modified. How can I solve this?
TLDR; instantiating objects holds a reference not a whole new object, causing terrains share height maps and not allowing for individual manipulation of terrain.
Then you'd need to manually copy the heightmap too and assign the copy to the new terrain. You can just Instantiate the texture2d used as the heightmap and it will create a duplicate.
Answer by 8o7wer · Aug 19, 2017 at 08:51 PM
I solved it by assigning a new TerrainData to the Terrain Object:
_terrainData = new terrainData();
terrain.terrainData = _terrainData;
Answer by UnityCoach · Aug 18, 2017 at 07:50 AM
Well, in fact this is a little specific to Terrain, but this is true for any Asset related component.
A copy of a Renderer will still point to the same Material for example.
You need to look into TerrainData.
I figured this out a while ago, I got other weird problems going on now.