- Home /

# 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.

```
![alt text][1]Mesh mesh = meshObject.GetComponent<MeshFilter>().sharedMesh;
RaycastHit hit;
for (int i = 0; i < mesh.vertices.Length; i++)
{
if (Physics.Raycast(new Vector3(mesh.vertices[i].x, 500f, mesh.vertices[i].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<MeshFilter>().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.

[1]: /storage/temp/102020-1.png

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.

I don't know how to change the pivot point of a script generated mesh

You can't. It's not possible in Unity, at least not without some custom functionality that edits the model file directly.

It would help if you would state what exactly are you trying to do. Also, have you tried bounds.min and .max?

Explain more of what I want to do, ok. I am trying to spawn a gameobject onto my terrain that I have created within script, I have two ways to do it but one causes great lag in the editor and in the game. And the other one starts from the center of my terrain mesh and looping from there giving me a incorrect bounds, it should start at the edge and start looping inward giving me the result that I want which is why I was wondering about the pivot change, but since that's not possible that's off the table I guess.

And as for the bounds.min and .max I don't see a way to loop through them since they are two values and not one. I need to get the x,z "bounds" of the entire mesh + that gives a smaller number than before so I still would not loop through all of the mesh. looping through the bounds.size gives me 94 this gives me 47

So I just did bounds.min.x and bounds.min.z and the bounds still starts in the center bounds.max gives me the same values as min so that's no beuno

**Answer** by Bunny83
·
Sep 17, 2017 at 10:15 AM

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[i].x, 500f, verts[i].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<Renderer>().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)
{
```

ps: you actually have another "error" with your vertices approach. The vertices are defined in **local space** but you directly use them as world space coordinates. This would only work when the actual mesh object is located at the world origin (has a position of 0,0,0) and it must not be rotated (so local and world space are identical).

The bounds approach works fine if the mesh isn't located at 0,0,0 but it also must not be rotated. If you may need it rotated you have to use Mesh.bounds which gives you local space coordinates and then transform every local space coordinate into world space. Either with transform.TransformPoint or by using the localToWorld matrix of the object.

I'm not really wanting to use the vertices method as it freezes up the editor and the game it was just a method I found when trying to spawn gameobjects onto this mesh. I will try out your solution to see if it works but thanks for posting :)

The bounds version works great and is what I was looking for thanks so much for the help :)

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.