How does tree instances work?

I can’t figure out how these work, here is the code I have.

var instances = new ArrayList();
if (index >= 0) {
	var instance = new TreeInstance();
	instance.position = new Vector3(ChunkPosX + x, height, ChunkPosZ + z);
	instance.color = Color.white;
	instance.lightmapColor = Color.white;
	instance.prototypeIndex = index;

	instances.Add(instance);
}
Terrain.activeTerrain.terrainData.treeInstances = instances.ToArray(typeof(TreeInstance));

The problem is that the trees doesn’t show in game. I have checked that the prototypes are working and they are working just fine.

NOTE: Index gets assigned by another part of the code, and its between the in game prototypes.

Uhm, it’s been a long time since I worked with TreeInstance, but if i recall correctly, when you set the position of the tree, you should scale it by the terrain size.
Something like this:

Vector3 currentTreeWorldPosition = Vector3.Scale(tree.position, terrain.size) + Terrain.activeTerrain.transform.position;

Now, again, it’s been a long time so I suggest you do further research on the topic.

Edit: Ok that is if you want to get the tree position in the terrain, not if you want to place it. Still, terrain coordinates are relative, so they go from 0 to 1. You should still scale the position of the tree you want to place for the inverse of the terrain size, I suppose.

You have to set
instance.heightScale = 1;
instance.widthScale = 1;
and at the end Flush the terrainData with:
terrain = Terrain.activeTerrain;
terrain.Flush();

EDIT: of course you can randomize the scales if needed :), the point is that you need to set them!