Terrain.SampleHeight(Vector3) always returns 0?

I’m trying to create spreading fire on top of the terrain but for whatever reason, SampleHeight() is returning 0 and causing all the fire to spawn under the terrain.

Also, ActiveTerrain is a declared variable because I was seeing if the problem was that Terrain.Active was returning null or something.

#pragma strict
var ParticleSystemMultiplier : ParticleSystemMultiplier;
var Before : float;
var SpreadFactorX : float = 1;
var SpreadFactorZ : float = 1000;
var Terraindata : TerrainData;
var ActiveTerrain : Terrain;

function Start () {

}

function Update () {
	if (Random.Range(1,100) == 1){
		var posx = Random.Range(-SpreadFactorX*100,SpreadFactorX*100)/100;
		var posz = Random.Range(-SpreadFactorX*100,SpreadFactorX*100)/100;
		var posy = ActiveTerrain.SampleHeight(Vector3(posx,0,posz))+ ActiveTerrain.transform.position.y;
		print(posx);
		print(posy);
		print(posz);
		Instantiate(transform.gameObject, transform.position+Vector3(posx,0,posz)-Vector3(0,transform.position.y,0)+Vector3(0,posy,0), Quaternion.identity);
	}
}

Any help?

Decent. 9 year old post, no reply. Got the same problem in Unity 2021.3.9f1. I also checked that activeTerain is not null.

ok hi maby if somebody has the same problem the problem was (for me) that the SampleHeight got called before the terrains height was set so than it wont work so make sure the terrain gets fully generated and than use SampleHeight