- Home /
Why vertex positions appear (0.0, 0.0, 0.0) ?
Hello. I print it in the code to see the locations of the vertexes in my hair object. But it is written as the position of all vertexes on the console (0.0, 0.0, 0.0). it shouldn't be like this. What could be the problem? Thanks in advance..
void Start()
{
meshfilter = GetComponent<MeshFilter>();
hairMesh = meshfilter.mesh;
verts = hairMesh.vertices;
foreach (var vert in verts)
{
Debug.Log(vert);
}
}
I tested your code on a simple cube and the vertex coordinates are displayed correctly in the console.
Answer by andrew-lukasik · May 06, 2021 at 11:14 AM
Rounding. Try:
Debug.Log($"( {vert.x:G9} , {vert.y:G9} , {vert.z:G9} )");
Edit: Replaced
G17
withG9
, as that's enough for floats.G17
is better suited fordouble
.
Note that Vector3 has an ToString version that takes a format descriptor. So you can simply do
Debug.Log(vert.ToString("G17"));
Just to clarify: By default the ToString method of the Vector3 type does round the components to 1 decimal place. So relatively small numbers would all round down to 0. Also keep in $$anonymous$$d that the vertices are specified in local space, not worldspace. So moving, rotating or scaling the object does not change the vertices.
Finally be careful when using the mesh property of the MeshFilter component as it will duplicate the mesh for this instance. If you just want to read the vertices, you should access the "sharedMesh" instead.
pps: I forgot to mention that in order to read the vertices of an imported mesh, that mesh need to be imported with read / write support enabled. If you did not do that explicitly, the mesh only exists in the GPU memory and you don't have access to the vertices.
Your answer
Follow this Question
Related Questions
Getting the world position of mesh vertices? 2 Answers
Creating a mesh for an overview map 0 Answers
Mesh.vertices are all 0? 1 Answer
UnityEngine.UI.Text characters mesh 0 Answers
Editing vertices 1 Answer