Mesh.vertices are all 0?
Asked Answered
G

1

0

I made this mesh in blender, and imported it into unity as an fbx. The vertices array has a bunch of verts on it, but they are all 0, 0, 0.

void OnGUI()
{  
    if (GUILayout.Button("test"))
    {
        plane = GameObject.Find("testPlane");  
        mesh=plane.GetComponent<MeshFilter>().sharedMesh;   
        for (int i=0; i<mesh.vertices.Length; i++) {Debug.Log(mesh.vertices); }
     }
}

Why would it be doing this?

Also this is an editor script in the editor folder.
public class editorTest : EditorWindow

Godown answered 4/9, 2023 at 11:35 Comment(0)
B
0

First and foremost never ever execute such a loop -.-

Each time you read the vertices property of the mesh class you will create a new copy of the vertices array and the vertices are copied from native code into the managed code array. Currently you create a new copy of your vertices array two times each iteration. So always cache that array:

Your next issue could be that since the default ToString override of Vector3 rounds the output to 1 decimal place you might not see your actual numbers if you created a very small mesh. This could easily happen when you imported the mesh with a very small scaling factor. So try printing the vectors with a higher precision. “F7” should be enough for most cases.

mesh=plane.GetComponent<MeshFilter>().sharedMesh;
var vertices = mesh.vertices; // read it once
for (int i=0; i < vertices.Length; i++)
{
    Debug.Log("Vertex #"+i+" = " + vertices[i].ToString("F7"));
}

If they are still all (0, 0, 0), do you actually see the mesh in the scene? How large does it appear in the scene? When click on the mesh field of the MeshFilter in the inspector, Unity should highlight the actual source mesh in the project so you can inspect the actual mesh and see its stats in the inspector. How many vertices / triangles does the mesh have?

Blakeley answered 5/9, 2023 at 5:35 Comment(1)

Thanks, the verts weren't exactly 0, but were really small numbers. It was because my fbx had 100 scale on it. I got the scale to 1 and everything is working now.

Godown

© 2022 - 2025 — McMap. All rights reserved.