Remove trees from the terrain during runtime?

Hello,

I have looked at several threads on how to remove a tree from a terrain (during the game), and I’ve written my own script. But I can’t figure out why my script won’t work. I should say that most of the examples are in C# (of course :roll_eyes:), and I mainly use Javascript/Unityscript. But that aside, I still am at a loss as to why it’s not working.

Here is my script:

//#pragma strict

public var amountOfWood : int = 100;

//Tree array for the terrain data
private var treeArray : TreeInstance[];
private var treeArrayJS = new Array();

private var distance : float;

private var terrain : TerrainData;
private var i : int;
private var thisTransform : Transform;

//The closest tree's position
private var closestTreePosition : float;
//Nearest tree to the collider
private var nearestTree : TreeInstance;


function Start () {

thisTransform = transform;
treeArray = Terrain.activeTerrain.terrainData.treeInstances;

treeArrayJS = new Array(treeArray);
//Debug.Log(treeArrayJS.length);

terrain = Terrain.activeTerrain.terrainData;

}

function Update () {


	if(amountOfWood <= 0) {
	
	
				// Our current closest tree initializes to far away
                var maxDistance : float = Mathf.Infinity;
                
                for (i = 0; i < treeArrayJS.length; i++)
                {
                	//Get the trees world position on the terrain
					var treePosition : Vector3 = Vector3.Scale(treeArrayJS[i].position, terrain.size);
		
                	//Check the distance between the tree collider and the tree's position
					closestTreePosition = (treePosition - thisTransform.position).sqrMagnitude;
            	
            		if(closestTreePosition < maxDistance) {
            			maxDistance = closestTreePosition;
      
                		nearestTree = treeArrayJS[i];
                		//Debug.Log(nearestTree);
                		
                					
                					
            		}
                
                }
 
                // Remove the tree from the terrain tree list
                treeArrayJS.Remove(nearestTree);
                treeArray = treeArrayJS.ToBuiltin(TreeInstance) as TreeInstance[];
	
	
	}



}

When this script executes, nothing happens. I don’t get any errors, but nothing at all happens.

Thanks for any help.

Throw in some Debug.Log statements at key points to make sure you’re even getting to them. If not, then figure out why.

When I debug.Log the Javascript array (after building it from the default treeInstances array), I get that there are 78 objects in the array. Which sounds right. 78 trees looks right to me as I look at my terrain.

Then when I Debug.Log the nearestTree variable in the “for” loop, I get about 78 logs like this:
UnityEngine.TreeInstance
UnityEngine.Debug:Log(Object)
ResourceHolder:Update() (at Assets/Scripts/ResourceHolder.js:54)

I can’t figure out why it won’t remove the closest tree to “x”.

Well I got it working thankfully. Took me about a day and a half to finally figure out why Unity no comprende, but thats why I hate and love programming lol.

My main stumbling block was this line at the bottom of the script:

treeArray = treeArrayJS.ToBuiltin(TreeInstance) as TreeInstance[];

For some reason, even though I referenced the actual treeInstance array at start, I actually had to “manually” reference it like this:

Terrain.activeTerrain.terrainData.treeInstances = treeArrayJS.ToBuiltin(TreeInstance) as TreeInstance[];

And then it worked. Why? I have no idea. Syntax wins again.

45 trial and error runs later…:shock::-x

Weird. Glad you got it sorted!

It’s because object variables in JS (and mostly in C#) are “references”, basically a thing which points to something else. Your “treeArray” variable is a different reference than Terrain.activeTerrain.terrainData.treeInstances. They are two different references both pointing to the same thing. When you reassign treeArray you make it point to a different array, but it doesn’t make Terrain.activeTerrain.terrainData.treeInstances point to it.