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 : Can I render nice thick lines with LineRenderer (without any artifacts)? - Questions & Answers - Unity Discussions

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*);* 
  •      }*
    
  •  }*
    
  •  else*
    
  •  lineRenderer.SetVertexCount(0);*
    

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.

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

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

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

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.