Create a TreePrototype causes a object null exception

Hi, well I’m creating a procedural world generation, and well I like to add some trees to my terrains (is in plurar because I’m using a terrain tile system)…

Well, the Docs and community hasn’t enough information for me, so, I started to code some hours ago, and I can’t resolve this problem…

My problem is that when I want to assing the Tree Prototype (this window) to my TerrainData object:


Using code, instead of using the window before, a null reference exception stops all the process… I don’t know exactly why, because Docs doesn’t say anything or how to assing TreePrototype to a TerrainData with code…

Anyway, there is the code that you can check and tell me what I’m doing wrong…

This code is for generate the terrainData:

public GameObject[] trees; //I used that because TreeProtype[] doesn't appear in Inspector...

TerrainData GenerateTile() {

	TerrainData tData = new TerrainData();
	//... Properties
	tData.treePrototypes = GenerateTreeFromGOs(trees);
	//... More properties
	return tData;


Well the function GenerateTreeFromGOs is that (and this is where I have the problem):

TreePrototype[] GenerateTreeFromGOs(GameObject[] gos) {
	TreePrototype[] trees = new TreePrototype[gos.Length];
	for(int i  = 0; i < trees.Length; i++) {
		trees*.bendFactor = m_bendFactor; //This part throw a null object exception*

trees_.prefab = gos*; //And this too…
return trees;
In this function I loop every assigned gameobject (via Inspector) and I add it to the prototypes…
So, how can I set the terrain Trees dynamically? Because, nobody (I think) has asked this before…
Thanks in advance.

This creates {gos.length} null references to treeprototypes:

TreePrototype[] trees = new TreePrototype[gos.Length];

You have to initialize them:

for(int i=0; i<gos.Length; i++) {
  trees *= new TreePrototype();*