- Home /
Remove trees dynamically - stuck with collider
I'm trying to remove trees dynamically.
I can remove the visible mesh/tree with TerrainData.treeInstances.RemoveAt(n) But I'm stuck with the colliders of the destroyed trees.
I've run out of ideas. Any one have a solution for this? Is it possible?2
Answer by duck · Apr 14, 2010 at 02:26 PM
I'm surprised that RemoveAt works at all for you with the treeInstances array, since it's a built-in array and doesnt' (as far as I know) support the RemoveAt function.
When I have modified or set the treeInstances array in the past, I've always built or modified them in a variable sized array (such as an ArrayList or List), and then dumped the entire new array back into terrainData.treeInstances as a whole, like this:
// get a copy of the trees, into a new dynamic collection List<TreeInstance> newTrees = new List<TreeInstance>(terrainData.treeInstances);
// modify the collection newTrees.RemoveAt(n);
// replace the trees back into the terrainData as a built-in array terrainData.treeInstances = newTrees.ToArray();
And also, make sure you are calling:
terrainData.RecalculateTreePositions();
after modifying the tree instances array.
Note
Some of these functions are undocumented. Any future updates of the unity engine might change or remove these functions from the API. This means your project may not work in future versions of the Unity editor, and webplayer builds may not work with future versions of the plugin.
Answer by jc_lvngstn · Nov 02, 2011 at 01:04 AM
Here is how:
// Now refresh the terrain, getting rid of the darn collider
float[,] heights = terrain.GetHeights(0, 0, 0, 0);
terrain.SetHeights(0, 0, heights);
You also may need to disable and reenable the terrain collider.
Here is a thread that gives more details: http://forum.unity3d.com/threads/110354-Finally-Removing-trees-AND-the-colliders
Answer by KHopcraft · Mar 30, 2010 at 09:55 PM
Sounds kind of like a bug. Try submitting a bug report.
These are undocumented functions - and therefore officialy unsupported, so a bug report is not applicable in this case.
Your answer
Follow this Question
Related Questions
Destroy Terrain Trees And Their Colliders 0 Answers
Picking Trees with Mouse 5 Answers
Removing terrain tree at runtime not updating collider 1 Answer
Billboard system 1 Answer