Mesh.MeshCombine example is erroring out.

using UnityEngine;
using System.Collections;

[RequireComponent(typeof(MeshFilter))]
[RequireComponent(typeof(MeshRenderer))]
public class Stuff : MonoBehaviour {
	void Start() {
		MeshFilter[] meshFilters = GetComponentsInChildren<MeshFilter>();
		CombineInstance[] combine = new CombineInstance[meshFilters.Length];
		int i = 0;
		while (i < meshFilters.Length) {
			combine_.mesh = meshFilters*.sharedMesh;*_

combine_.transform = meshFilters*.transform.localToWorldMatrix;
meshFilters.gameObject.SetActive(false);
i++;
}
transform.GetComponent().mesh = new Mesh();
transform.GetComponent().mesh.CombineMeshes(combine);
transform.gameObject.SetActive (true);
}
}*

This is the C# example. The only problem is the fact it errors out. Giving me this Warning(not an actual breaking error)
Combine mesh instance 0 is null.
UnityEngine.Mesh:CombineMeshes(CombineInstance[])
Stuff:Start() (at Assets/Stuff.cs:18)
It doesnt combine the meshes at all either. this is being used on a empty game object, with two cubes childing it._

This is because the GetCombineInChildren obtains the parent object as well.

The first CombineInstance is of the parent object, which is null.

To make it work, get rid of element 0 of the CombineInstance Array. Or.

	MeshFilter[] meshFilters = GetComponentsInChildren<MeshFilter> ();
	List<MeshFilter> meshfilter = new List<MeshFilter> ();

	for (int i = 1; i < meshFilters.Length; i++)
		meshfilter.Add (meshFilters *);*
  •   CombineInstance[] combine = new CombineInstance[meshfilter.Count];*
    
  •   int np = 0;*
    
  •   while (np < meshfilter.Count) {*
    
  •   	combine [np].mesh = meshfilter [np].sharedMesh;*
    
  •   	combine [np].transform = meshfilter[np].transform.localToWorldMatrix;*
    
  •   	meshfilter [np].gameObject.SetActive (false);*
    
  •   	np++;*
    
  •   }*
    

// rest of the code the same.

“with two cubes childing it.”

ok, and you use GetComponentsInChildren(); to get them, which is fine.

However, note that this function- will ALSO return MeshFilters found on the current game object (in addition to those found on children).

I see that you have specified: [RequireComponent(typeof(MeshFilter))] Which means that the STUFF object itself, MUST ALSO have a meshFilter component. My guess is that you do NOT have a mesh specified in there.

You could either fix that, or (recommend) bullet-proof the code a bit: and CHECK to confirm that the mesh filter actually specifies a mesh, before using it.

while (i < meshFilters.Length) {
   if(meshFilters*.sharedMesh!=null)*

{
//do stuff with this mesh filter’s sharedMesh
}
}