Blockquote void Update() {
Here is my solution:
void Update()
{
TerrainData terrain = Terrain.activeTerrain.terrainData;
ArrayList instances = new ArrayList();
foreach (TreeInstance tree in terrain.treeInstances)
{
distanceG = Vector3.Distance(gameObject.transform.position, tree.position);
distance = Vector3.Distance(Vector3.Scale(tree.position, terrain.size) + Terrain.activeTerrain.transform.position, transform.position);
if (distance < closeRange)
{
// the tree is in range - destroy it
print("1");
}
else
{
// tree is out of range - keep it
instances.Add(tree);
}
}
terrain.treeInstances = (TreeInstance[])instances.ToArray(typeof(TreeInstance));
}
After a long time it works