- Home /
Other
Stuck on how to get trees to show after calling CreateTerrainGameObject()
Hi,
I am trying to generate some trees for my terrain but with no luck. NOTE: when I inspect the treeInstances[] array I have the correct number of trees within the array but nothing appears on my screen. The problem is that this is incredibly hard to debug since the objects dont appear within the hierarchy.
Anyway I have a Main.cs
terrain = Terrain.CreateTerrainGameObject(TerrainGeneration.GenerateTerrain());
then I have a terrainGeneration class which holds these two relevant methods:
public static TreePrototype[] BindTreeToBrush()
{
TreePrototype[] trees = new TreePrototype[1];
trees[0] = new TreePrototype();
trees[0].prefab = Resources.Load("ScotsPineTypeA") as GameObject;
return trees;
}
public static TreeInstance[] TreeMaking(TerrainData myTerrain)
{
List<Vector3> treeLocations = TreePositioning(400, 3);
List<TreeInstance> trees = new List<TreeInstance>();
for(int i = 0; i < treeLocations.Count; i++ )
{
TreeInstance tree = new TreeInstance();
tree.prototypeIndex = 0;
Vector3 actualPosition = treeLocations[i];//Helper.TerrainToWorldPosition(myTerrain, 512, treeLocations[i]);
actualPosition.y = heights[(int)actualPosition.x, (int)actualPosition.z];
tree.position = actualPosition;
trees.Add(tree);
}
return trees.ToArray();
}
These two methods are being called inside the class before the GameObject is returned to main.
If I haven't given enough information please comment and I will supply more. Note I know I dont need to pass the TerrainData to the function but that doesnt matter.
Ow also the bind tree to brush function works because I can view that tree in the editor when I click the tree tab.
UPDATE: If I change tree.heightscale and tree.widthscale = 1 then I can get them to appear at the corner of the map in black. But they are not coloured nor are they in the correct positions even though when I print the Vec3 for position held in memory it gives me a location upon the terrain.
Thank you
Ow also if anyone has any tutorials on programatically adding trees using the Terrain class or any sample working code I would really appreciate it because the internet seems pretty dry for that kind of thing..
Follow this Question
Related Questions
Why does my Tree on Terrain not look like the Original Prefab it was made of? 0 Answers
Cannot place trees on terrain 0 Answers
Tree billboards clipping into terrain 1 Answer
Tree Creator Problems 1 Answer
Can't use tree brush tool 0 Answers