In my game i have multiple blocks that are combined into one mesh.
When I destroy one of the blocks and call the method to combine the meshes it doesn’t update the first time but when i destroy another block it updates the previous block breaking.
How do I fix it?
combine meshes method:
public void CombineMeshes()
{
DestroyImmediate(GetComponent<MeshFilter>().mesh, true);
DestroyImmediate(GetComponent<MeshFilter>().sharedMesh, true);
MeshFilter[] meshFilters = GetComponentsInChildren<MeshFilter>();
CombineInstance[] combine = new CombineInstance[meshFilters.Length];
Material mat = meshFilters[1].GetComponent<Renderer>().material;
int i = 0;
while (i < meshFilters.Length)
{
combine[i].mesh = meshFilters[i].sharedMesh;
combine[i].transform = transform.worldToLocalMatrix * meshFilters[i].transform.localToWorldMatrix;
meshFilters[i].GetComponent<Renderer>().enabled = false;
i++;
}
Mesh mesh = new Mesh();
mesh.CombineMeshes(combine);
GetComponent<MeshFilter>().sharedMesh = mesh;
GetComponent<Renderer>().material = mat;
GetComponent<Renderer>().enabled = true;
}
destroy block method:
public void demolish()
{
if (GameObjectsIn.Count > 0)
{
foreach (GameObject obj in GameObjectsIn)
{
obj.SetActive(true);
obj.transform.position = new Vector3(transform.position.x, transform.position.y + 0.5f, transform.position.z);
obj.GetComponentInParent<Rigidbody>().isKinematic = false;
}
}
if (GetComponentInParent<MeshCombiner>())
{
GetComponentInParent<MeshCombiner>().CombineMeshes();
}
Destroy(gameObject);
}