I’m generating terrain and then replacing the entire terrain.TreeInstances array with a new array of trees at runtime. Everything seems to work ok except that the trees lose their colliders. If I stop the game and go back into the editor, then turn off dynamic terrain generation and start the game again, the colliders will be there. If I then stop, turn on dynamic terrain generation again and start the game, the new trees won’t have colliders, and there will be invisible colliders where the old trees were in the editor. It seems like Unity must pre-cache the tree colliders somewhere and never updates them during runtime if you change the terrain or trees. I’ve tried including terrain.Flush and terrain.RefreshPrototypes in a few places hoping that would help but it doesn’t seem to. Does anyone have any ideas?
What type of collider are you using? Only capsule colliders can be used with trees.
Yes, they’re capsule colliders. As I said, they work fine if I place trees in the editor and then hit play, but they don’t work if I place the trees via script at run-time.
I figured out a workaround. It seems this has been a bug for a long time. I can get the colliders to update by resetting all of the heights to their current values, as mentioned in this post:
http://forum.unity3d.com/threads/110354-Finally-Removing-trees-AND-the-colliders
var heights = terrain.GetHeights(0, 0, 0, 0);
terrain.SetHeights(0, 0, heights);
1 Like