- Home /
Mesh.uv array index out of range
I've been following along on this tutorial on making a VR fruit ninja game. So far, mostly so good (I have another question posted about an unrelated, and non-showstopper issue).
My problem is that using the fruit object assets provided by the author, everything works fine. And using primitive objects created in Unity (i.e. a cube), everything works fine. But when I import my own objects, I get this error:
IndexOutOfRangeException: Array index is out of range.
BLINDED_AM_ME.MeshCut.Cut (UnityEngine.GameObject victim, Vector3 anchorPoint, Vector3 normalDirection, UnityEngine.Material capMaterial) (at Assets/BLINDED_AM_ME package/Scripts/MeshCut.cs:111)
SwordCutter.OnCollisionEnter (UnityEngine.Collision collision) (at Assets/SwordCutter.cs:17)
This is the line that seems to be causing the problem:
new Vector2[]{ _victim_mesh.uv[p1], _victim_mesh.uv[p2], _victim_mesh.uv[p3] },
I'm creating my objects in Blender, and upon some investigation I've found that the UV map from external programs can cause unexpected results. However, there is no UV map applied to these objects in Blender.
I've also read that the array size needs to be two, however the start of this loop looks like this
for(int i=0; i<indices.Length; i+=3){
p1 = indices[i];
p2 = indices[i+1];
p3 = indices[i+2];
So to me it looks like this error should also be thrown, but for some reason I don't get the issue if I sue objects created in Unity (like a cube) or use the assets provided by the tutorial.
I'm a total noob at this, can someone give me a bit of direction?
You should know how many UVs your models have and adopt the code to that, maybe just check the length of the uvs array too and break when inappropriate.
Answer by Bunny83 · Oct 03, 2017 at 02:02 AM
Your model simply doesn't have any UV coordinates. You probably didn't unwrap your object in blender. Maybe also have a look at this page
When you inspect a Mesh in the inspector it tells you what vertex attributes the mesh has. Here's an image of the default cube mesh:
The "red" is the number of vertices.
The "green" the number of triangles.
The "yellow" tells you that the mesh has a UV channel
The "orange" tells you that the mesh has a second UV channel.
If your mesh doesn't have the "uv", the mesh simply doesn't have uv coordinates. That's why the array is empty.
ps: Never do something like this:
new Vector2[]{ _victim_mesh.uv[p1], _victim_mesh.uv[p2], _victim_mesh.uv[p3] },
Using _victim_mesh.uv
will create a new array each time you access it. If you work with any mesh attributes you should always cache the arrays in local variables:
Vector2[] uv = _victim_mesh.uv;
new Vector2[]{ uv[p1], uv[p2], uv[p3] },
Of course this only works when your mesh actually has uv coordinates.
Thanks for the clear explanation. I didn't realize there were these layers of complexity! $$anonymous$$uch appreciated, and for the additional tip too (although that's not my code, its from the BLINDED_A$$anonymous$$_$$anonymous$$E asset pack).