Array index is out of range (94308)

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 <em>= new Color(ScaleAndBias(m2.normals<em>.x), ScaleAndBias(m2.normals_.y), ScaleAndBias(m2.normals*.z));*_</em></em> 

* }*

* 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);*
* }*

on which line did you get the error?

newColors <em>= new Color(ScaleAndBias(m2.normals<em>.x), ScaleAndBias(m2.normals_.y), ScaleAndBias(m2.normals*.z));*_</em></em>

1 Answer

1

m2 has fewer vertices than m1. You are using m1 vertex count to iterate m2.

for (int i = 0; i < m1.vertexCount; i++)
{
    newColors <em>= new Color(ScaleAndBias(m2.normals<em>.x), ScaleAndBias(m2.normals_.y), ScaleAndBias(m2.normals*.z));*_</em></em> 

}
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. [21359-výstřižek.jpg|21359] 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 Mesh 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?