[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._
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 } }