Terrain: Placing trees from code, but they are black...

Hi,

I’m adding trees to my terrain from code (at random positions), and they show up, though they are completely black…
When I place the same tree-prefab from the editor, they do have color…

In code I first create the terrainData and then create the terrain using Terrain.CreateTerrainGameObject(terrData);. Also tried first adding the treePrototypes to the terrainData, then creating the terrain, and then adding the trees directly to the terrain. I also flush the terrain after this (just to be sure, didn’t seem to help anything.)

Someone asked to clarify:
What I do is I create a terrainData, to this terrainData’s treePrototypes I add all the tree-prefabs (and set their bendfactor), then I place a few trees on the terrain at a random position by adding TreeInstances to terrainData.treeInstances. This gives my a terrain, with a bunch of black trees… (This all happens when the game is running.)
Then when I go to the scene view, select the created terrain, and then add TreeInstances using the painting-thing, I get properly coloured trees!
Though while writing this, I realize that I probably just forgot to set the color value in the TreeInstance when making them in code. I will check this when I get home (end of the week.)

Anyone any idea why this is?

Thanks!

From you question it’s not clear if you tried the tree by placing it by putting prefab on terrain or by terrain place trees brush. It looks like the shader used by the tree is not a terrain tree shader. Does the tree turn out black if you use the place tree brush or does it turn out correct then? The shaders used by the tree must be from the Nature group. The tree should be one mesh (with possibly different submeshes for the different materials). As your terrain instance is also created dynamically you could try if trees come out by placing them with brush while game is running in editor.

I forgot to post the fix I found, so let’s do that now.

Apparently you have to set a colour value for the trees, and by default this is black. If you set this to white, the normal texture colour of the trees will be shown.