Store mesh vertices in new Vector3[]?

hmmm....

Not quite sure why this is throwing a NullReferenceException

    var mesh : Mesh = GetComponent(MeshFilter).mesh;
    var vertices : Vector3[] = mesh.vertices;
    var normals : Vector3[] = mesh.normals;
    var newVertices : Vector3[];

    for(var i=0;i<vertices.Length;i++)
    {
        newVertices _= vertices*;*_
 _*}*_
_*```*_

1 Answer

1

You haven't initialized newVertices. But I don't see why you need newVertices anyway. That's what vertices is.

Also, use the generic form of GetComponent.

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

hmmm... ok, I think I'm getting it. But, how would one create a new mesh by getting every-other vertex from the first mesh? It would seem I'd have to cut vertices in half to get the right size, but I wouldn't be able to get every-other vertex, only the first half of the vertices. This is more specific than the question I asked.

–

I don't really understand what you wanna do. mesh.vertices returns already a copy of the vertex data. All changes made to the array data will not affact the mesh itself. You have to assign the array back to mesh.vertices to "apply" your changes. If you just want to copy all vertices in another array you have to create the new array with the same size of your source array before you can access an element. var newVertices : Vector3[] = new Vertex[vertices.Length];

–

ah, ok. Thank you both.

–

Oops, wait. Bunny63, what is "new Vertex[]"? I think that's my confusion.

–

That creates a new array. If you don't create the array you can't access the elements. An builtin array have to have a fix size that can't be changed. The size of the array have to be given in the square brackets when you create an array. And sorry i just made a little mistake ;) it should be = new Vector3[vertices.Length]; of course.

–