- Home /
Mesh.CombineMeshes error?
Please, help me to find out why on the following example script from the manual
using UnityEngine; using System.Collections;
[RequireComponent(typeof(MeshFilter))]
[RequireComponent(typeof(MeshRenderer))]
public class example : MonoBehaviour {
void Start() {
Component[] meshFilters = GetComponentsInChildren<MeshFilter>();
CombineInstance[] combine = new CombineInstance[meshFilters.length];
i = 0;
while (i < meshFilters.length) {
combine[i].mesh = meshFilters[i].sharedMesh;
combine[i].transform = meshFilters[i].transform.localToWorldMatrix;
meshFilters[i].gameObject.active = false;
i++;
}
transform.GetComponent<MeshFilter>().mesh = new Mesh();
transform.GetComponent<MeshFilter>().mesh.CombineMeshes(combine);
transform.gameObject.active = true;
}
}
I am getting the error: Assets/example.cs(9,61): error CS1061: Type UnityEngine.Component[]' does not contain a definition for
length' and no extension method length' of type
UnityEngine.Component[]' could be found (are you missing a using directive or an assembly reference?)
Answer by syclamoth · Jan 17, 2012 at 07:06 AM
Simple- there's no such thing as a '.length' member on arrays- the correct spelling is '.Length' with a capital L!
You should also look out for this line:
Component[] meshFilters = GetComponentsInChildren<MeshFilter>();
If the line is declared like that, there is nothing telling the compiler that the Components in that array will always be specifically MeshFilter-type components. Instead you should use the line:
MeshFilter[] meshFilters = GetComponentsInChildren<MeshFilter>();
This makes sure that the objects in the array can only be either MeshFilters or objects which derive from MeshFilter.
Thanks. Now I have to fight back another error: "Assets/example.cs(12,34): error CS1061: Type UnityEngine.Component' does not contain a definition for
shared$$anonymous$$esh' " I didn't expect the errors like that because I have just copied and pasted!
I think, if you just copy and paste, you should definitely expect errors. Copy-pasting code without understanding what it does is a sure path to failure!
In this case, it is because of your variable declaration-
Component[] meshFilters = GetComponentsInChildren<$$anonymous$$eshFilter>();
It should be more specific:
$$anonymous$$eshFilter[] meshFilters = GetComponentsInChildren<$$anonymous$$eshFilter>();
That way, the program will know exactly what kind of Component it should be using!
Your answer
![](https://koobas.hobune.stream/wayback/20220613063101im_/https://answers.unity.com/themes/thub/images/avi.jpg)