- Home /
Mesh.vertices is too small?
Hi,
I would like to use @Bunny83's script "custom line renderer", but because I remove the last vertices after one second, and "mesh.vertices" is a built-in array with a specific size, it says :
Mesh.vertices is too small. The supplied vertex array has less vertices than are referenced by the triangles array. UnityEngine.Mesh:set_vertices(Vector3[])
Is there a way to avoid this error? I have tried to replace the built-in array with a List, but Unity does not allow it. Would you have any idea?
Here is the link : http://answers.unity3d.com/questions/64716/can-i-render-nice-thick-lines-with-linerenderer-wi.html
Thanks
EDIT :
My function in another script is like that :
void Update () {
if(myPoints != null){
lineRenderer.SetVertexCount(myPoints.Count);
for(int i = 0;i<myPoints.Count;i++){
lineRenderer.SetPosition(i,myPoints[i]);
}
}
else
lineRenderer.SetVertexCount(0);
Answer by Fattie · Mar 24, 2013 at 10:47 AM
in a word ...
say a model has 100 triangles.
this means it will have 300 vertices.
[aside .... see note on "shared vertices" below]
recall that any TRIANGLES array simply has three entries for each triangle
so your TRIANGLES array will have 300 entries, and your vertices array will have 300 entries
say you want to take away 11 triangles
now, your TRIANGLES array now has 89 triangles, and is hence 267 length.
you now have 267 vertices so you'll need an all-new vertices array with 267 vertices.
note that the TRIANGLES arrray is the key thing you have to change, if you want to get rid of some triangles.
you "just have to" change the VERTICES each time you change the triangles for any reason.
however, don't forget that sometimes, you share vertices in thevertices array.
it's critical to understand that you do not have to share vertices, but you can if you want
So you might have two triangles (six verts). the TRIANGLE array would be and must be length six because you have six verts (two tris).
however, imagine you are sharing two of the vertices (draw a diagram)
so, if you wanted to do that, your verts array would be only four long.
again note that you DO NOT HAVE TO, for any reason, share vertices
this is a common misconception .. here is an ancient argument about it
http://answers.unity3d.com/questions/193695/in-unity-is-there-a-fast-way-to-find-nearby-triang.html
actually, it is much easier, when you are starting out, to not share vertices
quite simply: your verts array will always be the same length as your triangles array
So in a word .. it's the TRIANGLES array, you want to change, in this situation.
Hope it helps.
there are many discussions on these issues here
http://answers.unity3d.com/questions/329116/do-we-need-to-call-recalculatenormals-if-normal-ve.html
http://answers.unity3d.com/questions/352513/ways-of-modificating-mesh-triangles.html
BTW it's important to remember that when you access .vertices it CREATES A WHOLE COPY, it's a real gotchya in Unity when you're getting started. Cheers
Thanks Fattie, sorry I am a bit lost, in SetVertexCount, there is this line : m_Indices = new int[m_Points.Count*2];
and it is used in Update$$anonymous$$esh to create the triangle strips : m_$$anonymous$$esh.SetTriangleStrip(m_Indices,0);
. Should I add something like that in SetVertexCount : m_$$anonymous$$esh.triangles = new int[m_Points.Count*2];
? This triggers an error : "Null Reference". Could you help me with this? I have also edited my post to add the code I am using in the Update function. Thanks
I'm afraid I don't understand the specific code at all, sorry - but i'm sure someone else will help.
(I am too young to know about "vertex strips", that is more of a previous-generation idea.)
Essentially, if you get this error:
$$anonymous$$esh.vertices is too small...
your problem is you must set the .triangles array
the ,vertices array is merely an assistant to the .triangles array. you must understand, work with, and change the .triangles array. As a secondary issue, you would change the .vertices array.
read here for a number of basic examples
http://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$esh.html
hope it helps in some way until someone else helps specifically!
Answer by col000r · Feb 03, 2016 at 08:06 AM
The problem is that while it's okay to update the position of vertices, Unity doesn't react well to you changing the number of vertices, because it immediately checks it against the triangles and complains. Even though you were probably about to assign new triangles as well, it's too late - the Error is already there.
The trick is to do mesh.Clear(), and then set everything anew: vertices, triangles and whatever else you assigned.
Answer by Steven-Walker · Feb 11, 2014 at 01:04 AM
I ran into this error with a custom editor script that generates geometry dynamically. Even though my vertex and triangle counts were correct, the error was still occurring intermittently. The solution was to create a new Mesh every time when regenerating the polygons, rather than trying to modify an existing mesh. Also be sure to set the vertices before triangles.
great answer
solved by using prior to renewing: extruded$$anonymous$$esh.Clear();
I just ran into this. Quite surprising since docs are saying: "set vertices before triangles" but you can't set them earlier, because it's invalid :) Will do as Steven Walker suggested - recreate a mesh. However Unity docs should mention it.