Olivier Pons
Olivier Pons

Reputation: 15816

Unity: cannot combine mesh that does not allow access: Cube

When I call CombineMeshes() I get 431 times the error:

Cannot combine mesh that does not allow access: Cube
UnityEngine.Mesh:CombineMeshes(CombineInstance[])

I've already read this and this. I'm using Unity 2019.1.0f2 - hence if I want to use Blender, I'm forced to use Blender 2.80beta.

I've made a very simple cube (it can't be simpler actually) and I've exported this into Unity as a fbx file:

blender simple cube

I've called it 'Wall' like this:

wall blender object

From this I've made a prefab. Then I've made an empty object, created a script to generate the walls. It instanciates the prefabs to make walls, and this gives:

wall generated

But those prefab are simple cubes like this:

wireframe view

So I'd like to merge them. Unity has something ready for this: CombineMeshes() .

I've tried to adapt the code in the link CombineMeshes(), so here's my full script which is very simple: it instanciates + tries to merge everything at the end:

using System;
using System.Linq;
using UnityEngine;

public class CoinGenerator : MonoBehaviour
{
    public GameObject wallPrefab;
    public float gridSize = 40f;
    public float topPrefab = 60f;

    private void Start()
    {
        string[] ok = {
            "+------------+ +------------+",
            "|............| |............|",
            "|.+--+.+---+.| |.+---+.+--+.|",
            "|.|  |.|   |.| |.|   |.|  |.|",
            "|.+--+.+---+.+-+.+---+.+--+.|",
            "|............. .............|",
            "|.+--+.++.+-------+.++.+--+.|",
            "|.+--+.||.+--+ +--+.||.+--+.|",
            "|......||....| |....||......|",
            "+----+.|+--+.| |.+--+|.+----+",
            "     |.|+--+.+-+.+--+|.|     ",
            "     |.||..... .....||.|     ",
            "     |.||.+--- ---+.||.|     ",
            "-----+.++.|       |.++.+-----",
            "..........|       |..........",
            "-----+.++.|       |.++.+-----",
            "     |.||.+-------+.||.|     ",
            "     |.||...........||.|     ",
            "     |.||.+-------+.||.|     ",
            "+----+.++.+--+ +--+.++.+----+",
            "|............| |............|",
            "|.+--+.+---+.| |.+---+.+--+.|",
            "|.+-++.+---+.+-+.+---+.++-+.|",
            "|...||........ ........||...|",
            "+-+.||.++.+-------+.++.||.+-+",
            "+-+.++.||.+--+ +--+.||.++.+-+",
            "+......||....| |....||......+",
            "+.+----++--+.| |.+--++----+.+",
            "+.+--------+.+-+.+--------+.+",
            "+...........................+",
            "+---------------------------+"
        };
        MeshFilter[] meshFilters = {};
        for (int z = -14; z <= 16; z++) {
            for (int x = -14; x <= 14; x++) {
                char c = ok[30 - (z + 14)][x + 14];
                GameObject cp = null;
                if (c == '+' || c == '-' || c == '|') {
                    cp = Instantiate(wallPrefab, null, true);
                    MeshFilter[] m = cp.GetComponentsInChildren<MeshFilter>();
                    meshFilters = meshFilters.Concat(m).ToArray();
                }
                if (cp == null) {
                    continue;
                }
                cp.transform.position = new Vector3(
                    x * gridSize, topPrefab, z * gridSize
                );
            }
        }
        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;
            // hide the objects, they will be merges into one:
            //meshFilters[i].gameObject.SetActive(false); 
            i++;
        }
        transform.GetComponent<MeshFilter>().mesh = new Mesh();
        transform.GetComponent<MeshFilter>().mesh.CombineMeshes(combine);
    }
}

When I call CombineMeshes() I get 431 times the error:

Cannot combine mesh that does not allow access: Cube
UnityEngine.Mesh:CombineMeshes(CombineInstance[])

What am I doing wrong?

Upvotes: 6

Views: 6904

Answers (1)

Darren Ruane
Darren Ruane

Reputation: 2505

I suspect that the Cube model you have imported from blender does not have the Read/Write Enabled flag set to true.

Go into the Model Tab for your imported model and make sure that Read/Write Enabled is set to true and apply your changes.

If this flag is already set then the only other thing I can think of is that there might be a call to UploadMeshData() somewhere in your code (or perhaps in a package you have imported?). UploadMeshData() takes a boolean as a parameter, and if it's true, will set the mesh to be no longer readable by scripts.

Upvotes: 9

Related Questions