I am working on a project at the moment that uses a damaged mesh script to change meshes according to damage level …
and keep getting an error …
I am trying to isolate the cause …
It occurs to me that the importer could be changing the vertices count and therefore mucking up the routine … (I’ve modeled the object carefully modifying the original mesh … in the modeling environment the vertices are the same count) I don’t get it!
Is there a way of keeping the vertices count exact?
here’s the error messages
p.s.
although the error comes up when loading damaged mesh it’s not until I load the original mesh back in via a script that the models mesh shows messy structure … I really don’t get it!
Here’s a link to the project AzaRacer
in case you want to see the problem in action … you’ll need to click “single player” then “Start” … race the car around and acquire damage to the rear end of car … then exit game to the pre game level, click on “Edit Car” and repair car damage using the GUI provided. … sigh … I know that might be a hassle but it’s the only way to see the effect as I’m seeing it.
also here’s a sample of the relevant code I’m using
I’m working on a similar problem right now, have your tried to set the Triangles before the Vertices? In my case, but the same kind of code than you, if set the Vertices before the Triangles, I get errors as well.
But even if the Indices are assigned first, in my case it’s telling me " Failed setting triangles. Some indices are referencing out of bounds vertices."
So I have the impression I must create a temporary Mesh, change the values I need and then assign the temp mesh to my actual one. Which is a bit annoying here, because my function is called once per frame and I need extreme speed. I’m afraid it is going to generate a lot of garbage collect which is really slow!
However, when I am setting the Vertices before the Indices it tells me that " Mesh.vertices is too small. The supplied vertex array has less vertices than are referenced by the triangles array." and if I do otherwise it’s telling me " Failed setting triangles. Some indices are referencing out of bounds vertices."
Well, in either case I am modifying those arrays in order to change my mesh, but it seems that the mesh try to recreate itself when I change one or the other. Any idea how can I deal with that?
Oh, in your case (I am actually doing it) calling Mesh.Clear() might fix your problem.
EDIT: I read the Script Reference it is written:
3. Continously changing the mesh triangles and vertices: 1) call Clear to start fresh, 2) assign vertices and other attributes, 3) assign triangle indices.
It is important to call Clear before assigning new vertices or triangles. Unity always checks the supplied triangle indices whether they don’t reference out of bounds vertices. Calling Clear then assigning vertices then triangles makes sure you never have out of bounds data.
Even with that, I still get the error :
" Failed setting triangles. Some indices are referencing out of bounds vertices."
Ok for those who might be interested, my problem was that I was actually using subset of Vertices, UVs and Indices in order to show a scrolling text. However, I was doing that by copying a subset of each of the original arrays to a temp array from 0 to the length of my subset.
What happened then is that if I copy my vertices 124 to 2020 in a temp array from 0 to 1896, and I do the same for my Indices, such as 186 to 3030 into a temp array from 0 to 2844.
My index 3030 for example were still pointing to vertex 2020 which was changed to 1896 early on. So that’s why I’ve got the :
" Failed setting triangles. Some indices are referencing out of bounds vertices." error.