Question by
lachlan_orton · Mar 03, 2021 at 09:14 AM ·
c#combinemeshes
Combined meshes are invisible ("Combine mesh instance 0 is null")
I've tried combining all the meshes of my instantiated objects into one combined mesh, but they all appear "invisible". The material and mesh renderer exist and are enabled, but the objects appear invisible in the game and scene view. I also get the warning message "Combine mesh instance 0 is null." It should also be noted that the objects still appear individual in the hierarchy even after the CombineMeshes function has run.
Refer to the screenshot below for how it looks inside Unity.
And here is the function
private void CombineMeshes(GameObject myobj)
{
Debug.Log("CombineGroundMeshes has begun for object.");
Vector3 position = myobj.transform.position;
myobj.transform.position = Vector3.zero;
MeshFilter[] meshfilters = myobj.GetComponentsInChildren<MeshFilter>();
CombineInstance[] combine = new CombineInstance[meshfilters.Length];
int amt = 1; //amt -> amount
while (amt < meshfilters.Length)
{
combine[amt].mesh = meshfilters[amt].sharedMesh;
combine[amt].transform = meshfilters[amt].transform.localToWorldMatrix;
meshfilters[amt].gameObject.SetActive(false);
amt++;
}
myobj.transform.GetComponent<MeshFilter>().mesh = new Mesh();
myobj.transform.GetComponent<MeshFilter>().mesh.CombineMeshes(combine, true, true);
myobj.transform.gameObject.SetActive(true);
myobj.transform.position = position;
myobj.AddComponent<MeshCollider>();
}
And here is the function which calls the above function:
private void ObjectsToArray()
{
Debug.Log("ObjectsToArray has started.");
GameObject[] mygroundObjects = groundObjectsToCombine.ToArray();
GameObject[] mywallObjects = wallObjectsToCombine.ToArray();
Debug.Log("Objects have been converted from List{} to Array[].");
for (int g = 0; g < mygroundObjects.Length; g++)
{
CombineMeshes(mygroundObjects[g]);
Debug.Log("[GROUND] CombineMeshes has been called for object at position: " + g);
}
for (int w = 0; w < mywallObjects.Length; w++)
{
CombineMeshes(mywallObjects[w]);
Debug.Log("[WALL] CombineMeshes for wall has been called for object at position: " + w);
}
}
desktop-screenshot-20210228-18451608.png
(412.2 kB)
Comment