- Home /
Problem is not reproducible or outdated
Add Trees on Random Generated Terrain
I have used a script to make from a game object a random terrain and now I am trying to add random trees and then convert the gameObject to a Terrain
public GameObject terrainObject;
Terrain terrain = terrainObject.GetComponent<Terrain>();
But when I try to add trees on it with this code it doesn't work :
.............................
TreeInstance t = new TreeInstance();
t.color = Color.white;
t.heightScale = 1;
t.widthScale = 1;
t.lightmapColor = Color.white;
t.position = new Vector3(x, 0, z);
terrain.AddTreeInstance(t);
And I also get the following error when I try to do anything with the terrain gameobject
Object reference not set to an instance of an object
It seems that it doesn't transform the game object to a terrain.
Looks to me like "terrain" is null. Is that first bit of code outside any methods? You need to call GetComponent at the very least inside Start for it to work.
Follow this Question
Related Questions
Building generation in a voxel engine 1 Answer
Terrain Destruction(crater) 0 Answers
Dealing with placing large amounts of objects via script? 1 Answer
Is there a way to make Unity terrain react and "dig" away from certain objects? 0 Answers
LWRP - Placing markers on terrain and gameobject dynamically 0 Answers