- Home /
Tree Instance Array Replacement
Hey guys, I need to get one tree out of the treeInstances array, since there is only AddTreeInstance but no function to remove, I wrote it like this. It works fine, with 5000 trees I just have a little lag everytime a tree gets deleted.
Creating a whole new array everytime is ineffective but the array size is static, so I see no better way.
Do You know a better/faster way to do this?
List<TreeInstance> tempTreeInstance = new List<TreeInstance>();
tempTreeInstance.AddRange(Terrain.activeTerrain.terrainData.treeInstances);
tempTreeInstance.RemoveAt(id);
trees.RemoveAt(id); //array of interactive invisible trees at terrain tree locations
Terrain.activeTerrain.terrainData.treeInstances = tempTreeInstance.ToArray();
Thanks in advance!
Answer by cjdev · Aug 29, 2015 at 12:43 AM
Do you need to create a new tempTreeInstance list every time? Why not just store it and remove the id tree each time then create the array from that. I don't see a need to retrieve the terrainData every time if you already have it.
No I don't, thank you. Looks much better now.
if (treeInstances.Count == 0) treeInstances.AddRange(Terrain.activeTerrain.terrainData.treeInstances);
treeInstances.RemoveAt(id);
trees.RemoveAt(id);
Terrain.activeTerrain.terrainData.treeInstances = treeInstances.ToArray();
Answer by getyour411 · Aug 29, 2015 at 12:34 AM
There is no better way.
EDIT: Glad you got it worked out, but looking at the accepter Answer above, I see nothing really different; still creating a List (not shown, but obviously used), still removing and resetting using .ToArray().
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Object-Spawning Script? 1 Answer
What do you think of this? List, Vector 3 and Update 1 Answer
Split Textasset into List 1 Answer
Scaling and rotaion of terrain trees 1 Answer