Why mesh.verticles[i] Displays 0,0,0 ?

Hi I want to get possition of all mesh verticles in local space, but when I access them , they all seam to be 0,0,0, but there should be not

        MeshFilter = this.GetComponent<MeshFilter> ();

        for (int i = 0; i < MeshFilter.mesh.vertices.Length; i++) {
            Debug.Log ( MeshFilter.mesh.vertices[i]);
        }

What is the issue?

You need to get the mesh info from the GPU; it’s not a reference. (Also use lowercase for variables; using “MeshFilter” instead of “meshFilter” for the variable conflicts visually with the MeshFilter class and makes things confusing.)

var mesh = GetComponent<MeshFilter>().mesh;
var verts = mesh.vertices;

for (int i = 0; i < verts.Length; i++) {
    Debug.Log (verts[i]);
}

If you make modifications then you have to assign the verts back to the mesh. (Again, not a reference.)

–Eric

    void Update () {
        var mesh = GetComponent<MeshFilter> ().mesh;
        var verts = mesh.vertices;
 
        Debug.Log(verts[3]);


    }
}

still getiing 0,0,0
Debuging verts.length works

In that case, either verts[3] is 0,0,0, or close enough that the 1-decimal-place rounding of Vector3.ToString makes it appear so.

–Eric

1 Like