How to combine meshes with different materials?

I’m making a small minecraft clone for a new project, but I’ve encountered a problem, I am using this code to combine meshes in a chunk:

public void CombineMeshes(){
        MeshFilter[] meshFilters = GetComponentsInChildren<MeshFilter>();
        CombineInstance[] combine = new CombineInstance[meshFilters.Length];
        int 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,false);
        transform.GetComponent<MeshCollider> ().sharedMesh = transform.GetComponent<MeshFilter> ().mesh;
        transform.gameObject.active = true;
    }

but each block in the chunk(which are the children) have different materials and when i combine the mesh it uses the same material for the entire chunk. How can i combine meshes while using different materials for the different parts of the chunk?

“Why i want to combine the meshes”:
I want to combine meshes so it doesn’t have to update all the blocks renderers and colliders separately which will cause major lag, instead it will only update 1 mesh/chunk.

If I remind correclty you cannot combine two meshes that have different materials… actually a mesh with two material when sent to the gpu becomes two meshes. You’ll have to combine materials in some way.

I don’t know if it would help but…

I guess my question has changed to how to combine materials then.

Using atlas textures if you’re using a single shader for all your materials.

EDIT: I will expand on what I just said because it’s a little scarce. You’ll have to use the same material sharing the same texture atlas on all your meshes. It’s the UVs of your meshes that will determine which part of that texture atlas to use. You can change the UVs of your meshes by script… I’m not sure it is the right way to go for your use case though… just answering to your question in the limited context of combining mesh.

I think i’m going about this the wrong way because i just though that if i want water in the game it has to be a trigger but i’m setting the whole chunk as a solid collider, so what would you guys do about this?

Why don’t you use multiple materials applied on different submeshes?

What do you mean by that? There’s only 1 mesh for each chunk.

What he means is that its not quite true that 1 mesh = 1 material. Meshes can be divided into submeshes, which can each have their own materials. Essentially its the same as having several seperate meshes, just combined into 1 for convenience. The combinemesh feature as far as i’m aware doesn’t support submeshes/multiple materials, so it just combines everything into 1 submesh. It is technically possible to combine them so that each mesh you started with is a different submesh and can keep its own seperate material slot in the combined mesh but you’ll either have to write the function to do that yourself or look for an existing solution in the asset store.

So i might as well just have every block as a separate object then instead of chunks if each mesh can only have 1 material?

No, if you have every block as a seperate gameobject then your going to get abysmally bad performance. Draw large sections of blocks in chunks and use texture atlasing to make them have the textures you need to look different. You don’t need a different material on every block type, just a different texture.

What is texture atlasing and how do i use it?

Yeah Why No Reply To Use Texture Atlasing??

Please don’t reply to five-year-old threads with random questions about why nobody replied.

For one, spamming old threads is actually against forum rules.

For two, “texture atlasing” is extremely well documented in the general sense, and given the context of the comment it replied to (combining meshes and minimizing drawcall batches for better performance), it’s pretty obvious that nobody here is going to retype the internet worth of knowledge around texture atlasing.

If you actually have an issue, please start your own new post. It’s FREE!

When you do, here is how to report your problem productively in the Unity3D forums:

http://plbm.com/?p=220

How to understand compiler and other errors and even fix them yourself:

https://forum.unity.com/threads/assets-mouselook-cs-29-62-error-cs1003-syntax-error-expected.1039702/#post-6730855

If you post a code snippet, ALWAYS USE CODE TAGS:

How to use code tags: https://discussions.unity.com/t/481379