getting y value for object at position x,z

i am trying to instantiate a bunch of trees in specific places. i have the x and z values for each tree, but i am trying to figure out how to get the y value of the terrain at that position so the trees rest nicely on the ground. any suggestions?

You could take the x and z positions, pick a y that you know will be above the ground and then cast a ray down at the ground and use the y component of the resulting hit.

yea the only problem with that is im instantiating about 10,000 trees and they constantly die and new ones regrow after a certain amount of time in the game. it would be alot of work to put in 10,000 rays and impossible after the game starts to get the correct y value of new trees.

by a lot of work do you mean a lot of work for the processor?

and why would it be impossible after the game starts to find the y value for new trees?

haha wait im sorry ive been up for 36 straight hours so bare with me. to instantiate new rays would obviously not be impossible to do along with new trees. however, i think that instantiating so many rays would be too much work for the processor, especially considering this game will run on mostly on old computers in local middle schools and high schools (its a learning tool). theres only one way to find out tho. ill let u know how it goes thanks.

in case anyone is following this, here’s the way i ended up doing it:
var emptyVector = Vector3(x,0,z);
var y = Terrain.activeTerrain.SampleHeight(emptyVector);

tree = Instantiate(pineTree, Vector3(x, y, z), Quaternion.identity);