x


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

more ▼

asked Mar 09 '10 at 11:11 PM

Jademyr gravatar image

Jademyr
41 1 1 7

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

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.

more ▼

answered Apr 14 '10 at 02:26 PM

duck gravatar image

duck ♦♦
41k 92 148 415

(comments are locked)
10|3000 characters needed characters left

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

more ▼

answered Nov 02 '11 at 01:04 AM

jc_lvngstn gravatar image

jc_lvngstn
451 24 29 40

(comments are locked)
10|3000 characters needed characters left

Sounds kind of like a bug. Try submitting a bug report.

more ▼

answered Mar 30 '10 at 09:55 PM

KHopcraft gravatar image

KHopcraft
204 1 9

These are undocumented functions - and therefore officialy unsupported, so a bug report is not applicable in this case.

Apr 14 '10 at 02:29 PM duck ♦♦

Oh, thank. I see the problem. Sorry.

Apr 21 '10 at 04:37 PM KHopcraft
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1705
x1478
x764
x229
x91

asked: Mar 09 '10 at 11:11 PM

Seen: 2860 times

Last Updated: Jul 06 '12 at 07:28 PM