- Home /
Why is my Tree Removal so low-performing when placing a GameObject nearby
Hi
In my City Builder I have a lonely Island which is not urbanized at all. Which means there are lots of trees. When placing a building on the ground I want the trees to be removed. I actually found out a way to do this. The problems:
It will only work with the first building
Unity freezes while doing it (and while not doint it on the follow-up buildings)
Here's my code:
foreach (TreeInstance tree in Terrain.activeTerrain.terrainData.treeInstances)
{
distance = Vector3.Distance(
Vector3.Scale(tree.position
, Terrain.activeTerrain.terrainData.size)
+ Terrain.activeTerrain.transform.position
, _temporaryBuilding.transform.position);
if (distance > 25f)
{
//Terrain.Destroy(tree); TODO is there a way to use destroy?
treeInstances.Add(tree); // treeInstances is an ArrayList declared earlier
}
Terrain.activeTerrain.terrainData.treeInstances
= (TreeInstance[])treeInstances
.ToArray(typeof(TreeInstance));
}
I can see, that this solution is quite low-performing. Because I always have to add every single tree again. Or at least I guess this might be the reason. But actually I've seen games working just fine with that solution...
But mine is freezing AND the removal won't work on a second try. Only for the first building.
Answer by keinabel · Mar 05, 2013 at 05:53 PM
The problem was
Terrain.activeTerrain.terrainData.treeInstances = (TreeInstance[])treeInstances .ToArray(typeof(TreeInstance)); which I accidentically put inside the foreach loop.
Answer by deltamish · Jan 31, 2013 at 04:18 PM
HI,first try places a group of trees directly as prefab and try removing it using GameObject.SetActive = false;(dont use Destroy Destroying an object calls GarbageCollector which takes a bit of high memmory.This may the cause unity to freeze if there a lot to destory and has only little memmory
) If you want an Script that uses GameObject.SetActive = false; please do feel free to ask
Update Hi here is the sript for the mouse This script is for birdeye view(Top view)
//omitted Using Unity Engine..etc
public class BuildingPlacer : MonoBehaviour {
public Transform BuildingA;
public Transform BuildingB;
private Transform tgtbuilding;
int treelayer;
void Update(
treelayer = 1<<9 ;` /// -->9 Number of the Tree layer
treelayer = ~treelayer;
if(Input.GetKeyDown(KeyCode.A)){
tgtbuilding = BuildingA;
}
if(Input.GetKeyDown(KeyCode.B)){
tgtbuilding = BuildingB;
}
if(tgtbuilding){
if(Input.GetButton("Fire1")){
Ray Pos= Camera.main.ScreenPointToRay(Input.mousePosition);///Note your Camera should be Tagged MainCamera
RaycastHit hit = hit;
if(Physics.Raycast(Pos,out hit,10000,treelayer) && hit.normal.y > 0.999){
if(hit.collider.tag == "PlaceableArea"){
hit.collider.SendMessage("RemoveTrees",SendMesageOptions.DontRequireReceiver);
PlaceBuilding(tgtbuilding,5,hit.point);
}
}
}else{
Debug.Log("Select the building by pressing A or B");
}
}
IEnumerator PlaceBuilding(Transform obj,float wait,Vector3 pos2){
yield WaitForSeconds(wait);
Instantiate(obj,pos2,Quaternion.FromToRotation(Vector3.up,hit.normal);///for Uneven terrain use Quaternion.FromToRotation(Vector3.up,hit.normal) else use obj.rotation
}
}
}
now Create a box and scale on y axis until it looks like a plane and then tag it PlaceableArea now place Trees overthem manually (Like you place character i.e drag and drop) and create a new layer(le this be on number 9) name them treelayer(or anything you want) and mark the trees and anyobject other than the box tagged PlaceableArea as treelayer
attach this script to the box
//omitted using unity engine...
public class TreeRemover(){
public GameObject[] Trees;//Assign trees place above the box here
void RemoveTrees(){}
foreach(int k =0;k<Trees.length;k++){
Trees[k].SetActive = false;
}
}
}
Hope it works the way you wanted
Sorry for my misconception.What i meant was to place trees manually(not using terrain engine) like when you drag an object(lets say an character) from the project panel into the scene view.Hope you got what i am trying to say here
There are several ways in which you can achieve this One of them is using raycast .you can also do this by using angle calculation ,and also using collision detection.Tell me which method you shall use and i shall write the script
By angle calculation means you should calculate the angle between each tree and the y axis of mouse pointer in that way you can get which trees are directly below it.now before i write the script i need to ask you a question
1.do all buildings have same area(same length and breadth)
hi @keinabel can you tell me wether all buildings have same area(same length and breadth) or not so that i can write the script for you
Very well That's good I shall write the script tomorrow cause i am kinda busy with my studies at the movement.
If you wnat the script today itself then i can give you a rough idea of the simple system you are going to use
Just Raycast downwards (-Vector3.up) and see if it hits a plane tagged "Something like area " then if you want to place a building there then send message to the parent of the object or the object itself asking ti remove the trees which is child of the plane
Hope you understand the concept
Reply
Your answer
![](https://koobas.hobune.stream/wayback/20220613091925im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Terrain Tree Placement 0 Answers
Remove Trees during runtime 1 Answer
Remove trees dynamically - stuck with collider 3 Answers
Placing tree objects with Terrain or Polybrush which one is useful ?? 0 Answers