Destroying or Deleting Trees at runtime?

Hello everyone,

At the moment what I am trying to do is delete a tree (that is created using Unity’s Place Trees) when a player clicks on it.

The code I have at the moment is rudimentary and is just to test and see if it is possible. I am able to identify the tree in the terrain.treeInstances array, but I have been unsuccessful at removing it.

	void Update () 
	{
	
		if(Input.GetButtonDown("Fire1"))
		{
			CutTree();
		}
        }
void CutTree()
{
		
	TerrainData terrain = Terrain.activeTerrain.terrainData;
	Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
	RaycastHit hit;
	float distance = -1;
	bool foundTree = false;
	int treeIndex = 0;
		
	if(Physics.Raycast(ray, out hit))
	{
		 distance = Vector3.Distance(hit.point, transform.position);
		
		// Checks to see that where we clicked is within 10 meters.
		if(distance > 0  distance < 10)
		{
			// Goes through the tree array and checks to see if there a tree is nearby
			for(int i=0; i < terrain.treeInstances.Length; i++)
			{
				//Is the tree within 2 meters of where we clicked?
				if(Vector3.Distance(hit.point, Vector3.Scale(terrain.size, terrain.treeInstances[i].position)) < 2)
				{
					foundTree = true;
					treeIndex = i;
					Debug.Log("FOUND IT.");
				}
			}
}

I was using the foundTree and treeIndex in an attempt to shift all treeInstances elements after the index down one, effectively removing the tree, but a) nothing happens, and b) even if it were to, you cannot set the final element of the array to null or otherwise delete the last element.

Does anyone have any suggestions, or have had any success at deleting trees at runtime? The only thing I can think of is creating a script that instantiates trees at runtime and self-manage them in a List, thus allowing me to use Object.Destroy.

Thanks in advance,

I was trying to cutting trees as well, but with a simpler approach, that doesn’t work as well. I was expecting that during runtime each Tree in the terrain was a standalone GameObject, but it seems it is not the case :frowning:

My approach was basically:

function OnControllerColliderHit (hit : ControllerColliderHit) {
var body : GameObject = hit.gameObject;

and now, tryint to find out if GameObject is, or not, a tree, and issuing Destroy on it.
But after some debug, I found that this body is always the terrain :frowning:

Cheers
ambs

I assume if you create each tree individually as a gameObject, performance will take a hit?

I bet. A big hit even… :smile:

Did somone get it done to delete trees, grass or detailed objects at runtime ?

LoL this is easy and im a noob. Just use this:

its in JavaScript

var tree : Transform;

if(Vector3.Distance(tree.transform.position, transform.position) < 5){
Destroy(gameObject);
Anything else you want to happen. for some reason you have to use destroy before other stuff
}

Its attached to the player!

Just make a prefab for the tree then save the prefab. Then selected prefab as the tree var. Then youre good to go. Can sometimes be glitchy but should basically work

Correct me if im wrong but the trees in the terrain editor dont show up in the project or hierarchy so trying to destory there transform wouldnt work since they are, in essence, part of the terrain object. Pease correct me if im wrong, I am unfamiliar with unity’s terrain system.

Im actually not sure. I gota test that. But it works if you place the trees manually