Getting mesh length (x,z)

So I am currently trying to spawn world objects onto my procedural mesh but the two solutions that I have come up with work but not the way I want them to.

Example 1.
Getting all the vertices associated with the mesh.

        [102020-1.png*_|102020]Mesh mesh = meshObject.GetComponent<MeshFilter>().sharedMesh;
        RaycastHit hit;
        for (int i = 0; i < mesh.vertices.Length; i++)
        {
            if (Physics.Raycast(new Vector3(mesh.vertices_.x, 500f, mesh.vertices*.z), Vector3.down, out hit, Mathf.Infinity))*_

{
if (hit.point.y > foliage[0].minHeight && hit.point.y < foliage[0].maxHeight)
{
spawnLocations.Add(hit.point);
}
}
}
This works perfectly except that it causes a great amount of lag for a bit of time. I would not have to worry about the lag if it wasn’t noticeable or was very short but it is noticeable and it’s not short so I sadly can’t use this method along side generating my terrain.
Example 2.
Getting the bounds of the mesh

Mesh mesh = meshObject.GetComponent().sharedMesh;
RaycastHit hit;
for (int x = 0; x < mesh.bounds.size.x; x++)
{
for (int z = 0; z < mesh.bounds.size.z; z++)
{
if (Physics.Raycast(new Vector3(x, 500f, z), Vector3.down, out hit, Mathf.Infinity))
{
if (hit.point.y > foliage[0].minHeight && hit.point.y < foliage[0].maxHeight)
{
spawnLocations.Add(hit.point);
}
}
}
}
Again this works but this as well, but the result of this is not in anyway what I want out of this, it seems to be getting the origin of the mesh and getting the bounds starting from there. This would not be a problem but I don’t know how to change the pivot point of a script generated mesh.
_*
I’ve tried everything I know to no avail and I will continue to keep trying but if I could get some help/advice with this it would be greatly appreciated.

Ok lets tackle your questions one by one as they seem to aim for something very different.

Is there any particular reason why you want the spawn position at the vertices of the mesh?

If you want to use the vertex positions you have to store the vertices array in a local variable. Currently you access the vertices property three times each iteration. Every time you read the vertices property Unity will allocate a new array with all vertices. So you end up with tons of garbage that need to be collected. Also the allocation is quite slow and unnecessary.

So you can do this:

Vector3[] verts = mesh.vertices; // allocate array once
for (int i = 0; i < verts.Length; i++)
{
    if (Physics.Raycast(new Vector3(verts_.x, 500f, verts*.z), Vector3.down, out hit, Mathf.Infinity))*_

// …
This should probably improve your performance by a factor of 100.
If you want to use the bounds instead, don’t use the Mesh.bounds. Mesh.bounds is defined in the local space of the mesh. Use Renderer.bounds.
Also you shouldn’t start your x and y at “0” but at bounds.min and let it end at bounds.max. Specifically something like this:
Bounds bounds = meshObject.GetComponent().bounds;
Vector3 min = bounds.min;
Vector3 max = bounds.max;
for (float x = min.x; x < max.x; x++)
{
for (float z = min.z; z < max.z; z++)
{
Keep in mind that “x++” increments in whole world units. You may want to define some sort of step size to control how fine your grid is. Be careful to not use a too small step size. Imagine the mesh has a bounds of min.x = -5 and max.x = 5. If you use a step of “0.1” for example you get “100” steps along the x axis. If you have the same for z you end up with 100*100 == 10000 points. While using a step size of “1” you just have 10 * 10 == 100 points across the mesh. Tweak it as you need it.
for (float x = min.x; x < max.x; x+=xStep)
{
for (float z = min.z; z < max.z; z+=zStep)
{