Clearing the TreeInstance array removes all trees perminantly

Hey guys.
I wanted to use the terrain tree painting tools to create nice woods. However the terrain trees don’t offer all I want so I have an initializer that runs through the TreeInstance array and creates full on GameObject trees. However, when I set the TreeInstance array to an empty array at run time- to get rid of the terrain trees, this removes the trees from the designer too.

I’ll include the script that is attached to the terrain.

public class TerrainInitaliser : MonoBehaviour 
{ 
	public ArrayList treeArray;
	
	// Use this for initialization
	void Start () 
	{
		// Grab the tree array from the terrain
		treeArray = new ArrayList(Terrain.activeTerrain.terrainData.treeInstances);
		int originalCount = treeArray.Count -1;
		Debug.Log("Tree instances: " + originalCount);
		
		// Substitute all the trees for game objects
		for(int i = 0; i <= originalCount; i++)
		{
			GameObject newTree = null;
			
			switch(((TreeInstance)treeArray[0]).prototypeIndex)
			{
				case 0 : 
				{
					newTree = GameObject.Instantiate(Resources.Load("PoC Prefabs/OakTree")) as GameObject;
				}; break; 
				
				case 1 : 
				{
					newTree = GameObject.Instantiate(Resources.Load("PoC Prefabs/PalmTree")) as GameObject;
				}; break; 
				
				default : 
				{
					return;
				}
			}
			
			// Set the properties
			newTree.transform.position = new Vector3(
				Terrain.activeTerrain.terrainData.size.x * ((TreeInstance)treeArray[0]).position.x, 
				Terrain.activeTerrain.terrainData.size.y * ((TreeInstance)treeArray[0]).position.y, 
				Terrain.activeTerrain.terrainData.size.z * ((TreeInstance)treeArray[0]).position.z
			);
			
			newTree.transform.localScale = new Vector3(
				((TreeInstance)treeArray[0]).widthScale, 
				((TreeInstance)treeArray[0]).heightScale, 
				((TreeInstance)treeArray[0]).widthScale
			);
			
			// Remove this tree
			treeArray.RemoveAt(0);
		}
		
		// Remove the built-in trees from the terrain
		TreeInstance[] tmpArray = new TreeInstance[0];
		treeArray.CopyTo(tmpArray);
		Terrain.activeTerrain.terrainData.treeInstances = tmpArray;
		
		// Refresh the terrain
		float[,] heights = Terrain.activeTerrain.terrainData.GetHeights(0, 0, 0, 0);
		Terrain.activeTerrain.terrainData.SetHeights(0, 0, heights);
		
		Debug.Log("Done");
	}
}

That is correct.

The terrainData is being modified at runtime and does not realize it is supposed to change back to it’s unaltered state. You have to tell the terrainData to reset at runtime exit. You can fix by adding a script to your terrain.

public class TerrainScript : MonoBehaviour {

	private Terrain terrain;
	public TreeInstance[] originalTrees;

	void Start () {
		terrain = GetComponent<Terrain> ();
		//save original trees
		originalTrees = terrain.terrainData.treeInstances;
	}

	void OnApplicationQuit() {
		// restore original trees
		terrain.terrainData.treeInstances = originalTrees;
	}
}