Can´t get terrain size throuth code.

What im precisely trying to do is to spawn a few objects in random within the margins of the x, and z of the terrain size.

So Im using :

public static Terrain activeTerrain;
To store the activeterrain to a terrain

And :

Vector3 Dsize = activeTerrain.terrainData.size;

This gives a “nullPointerExcepcion” error .

Can someone give me an example to how to get the current active terrain or at least one terrain and how to get the size of it? Or is there any other way to set an object to be spawn within the limits of the terrain?

Thanks

You cannot drag inspector references into public static variables. Unless you have other code that searches for and sets that static variable, it will be null.

In your script make it a public Terrain activeTerrain; and then be sure to drag a reference to the terrain in the scene into the script.

Yeah, it was a silly post actually.

I´ve solve it. All i have to do was make :

On start:
Dsize = Terrain.activeTerrain.terrainData.size;

And that´s it

1 Like