- Home /
Array index is out of range
I have one question, I have the script to edit all children in the object, from second object. Both objects have the same number of children, of course, the same material ... but after running I got error "Array index is out of range." It's strange, because if I set the same object for regular and smoothed, here's no error as well if objects have only one child ... I tried foreach instead of for, but it did not help ... Any ideas?
public GameObject regularMesh;
public GameObject smoothedMesh;
private float ScaleAndBias(float normal)
{
return (normal * 0.5f + 0.5f);
}
void Start (){
MeshFilter[] allChildren = new MeshFilter[regularMesh.transform.childCount];
allChildren = regularMesh.GetComponentsInChildren<MeshFilter>();
MeshFilter[] allChildren2 = new MeshFilter[regularMesh.transform.childCount];
allChildren2 = smoothedMesh.GetComponentsInChildren<MeshFilter>();
for (int m = 0; m < allChildren.Length; m++){
Mesh m1 = allChildren[m].mesh;
Mesh m2 = allChildren2[m].mesh;
GameObject go = new GameObject(allChildren[m].transform.name);
go.transform.parent = this.transform;
MeshFilter meshFilter;
meshFilter = go.AddComponent<MeshFilter>();
MeshRenderer meshRenderer;
meshRenderer = go.AddComponent<MeshRenderer>();
meshRenderer.materials = allChildren[m].renderer.materials;
Mesh newMesh = new Mesh();
meshFilter.mesh = newMesh;
Color[] newColors = new Color[m1.vertexCount];
for (int i = 0; i < m1.vertexCount; i++)
{
newColors[i] = new Color(ScaleAndBias(m2.normals[i].x), ScaleAndBias(m2.normals[i].y), ScaleAndBias(m2.normals[i].z));
}
newMesh.vertices = m1.vertices;
newMesh.triangles = m1.triangles;
newMesh.normals = m1.normals;
newMesh.uv = m1.uv;
newMesh.colors = newColors;
}
//ObjExporter.MeshToFile (meshFilter, Application.dataPath + "//Test.obj", true);
}
newColors[i] = new Color(ScaleAndBias(m2.normals[i].x), ScaleAndBias(m2.normals[i].y), ScaleAndBias(m2.normals[i].z));
Answer by VildNinja · Jan 29, 2014 at 07:11 PM
m2 has fewer vertices than m1. You are using m1 vertex count to iterate m2.
for (int i = 0; i < m1.vertexCount; i++)
{
newColors[i] = new Color(ScaleAndBias(m2.normals[i].x), ScaleAndBias(m2.normals[i].y), ScaleAndBias(m2.normals[i].z));
}
I assume that your smooth model is smoothed from a 3D program. That means that a lot of the vertex can be reused for several polygons, as they now have a shared normal. Where none smooted models have three unique vertex per triangle. Your polycount may still be the same, but the vertex and normal count can differ.
For smoothing, I use this in Unity.
This means that just change m1.vertexCount
to m2.vertexCount
?
Well yes, to fix that error.
But I am not quite sure what you are trying to do. you want the normals from m2 to be the colors of m1? You can't do it like that because m1 have more vertices than m2, so you will have to few colors.
Just to be perfectly clear m1 and m2 are from the exact same model right? m2 is just smoothed?
Yes, it is the same model. I want to do this:
http://answers.unity3d.com/questions/625968/unitys-outline-shader-sharp-edges.html#answer-626001
It is working, but only for one full object with one $$anonymous$$esh Filter, like box, cylinder,... not for objects with children (it is actually almost all objects)
Hmm make sure that m1.triangles.Length is the same as m2.triangles.Length and that m1.vertex.Length is the same as m2.vertex.Length. The most important thing is that triangles.Length are the same.
Lenght of triangles is the same - 240, but m1 have 99 vertices and m2 87... What to do?
Your answer
![](https://koobas.hobune.stream/wayback/20220613132031im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
[solved] passing energy from one object to another 0 Answers
Generic list of objects, then distance to those objects? (js) 1 Answer
print(var a = object) = null while print(oject) = what i want 2 Answers
How to store gameobjects in an array 2 Answers
How do I check if all objects in an array are destroyed? 2 Answers