title: Unity: cannot combine mesh that does not allow access: Cube
body: <p>When I call <code>CombineMeshes()</code> I get 431 times the error:</p>
  
  <pre><code>Cannot combine mesh that does not allow access: Cube
  UnityEngine.Mesh:CombineMeshes(CombineInstance[])
  </code></pre>
  
  <p>I've already read <a href="https://stackoverflow.com/questions/53835715/mesh-combiner-in-unity">this</a> and this.
  I'm using Unity <code>2019.1.0f2</code> - hence if I want to use Blender, I'm forced to use Blender <code>2.80beta</code>.</p>
  
  <p>I've made a very simple cube (it can't be simpler actually) and I've exported this into Unity as a <code>fbx</code> file:</p>
  
  <p><a href="https://i.stack.imgur.com/vJdyi.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vJdyi.png" alt="blender simple cube"></a></p>
  
  <p>I've called it '<code>Wall</code>' like this:</p>
  
  <p><a href="https://i.stack.imgur.com/ojmkO.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ojmkO.png" alt="wall blender object"></a></p>
  
  <p>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:</p>
  
  <p><a href="https://i.stack.imgur.com/Da3TT.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Da3TT.png" alt="wall generated"></a></p>
  
  <p>But those prefab are simple cubes like this:</p>
  
  <p><a href="https://i.stack.imgur.com/bhYz3.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/bhYz3.png" alt="wireframe view"></a></p>
  
  <p>So I'd like to merge them. Unity has something ready for this: <a href="https://docs.unity3d.com/ScriptReference/Mesh.CombineMeshes.html" rel="nofollow noreferrer"><code>CombineMeshes()</code></a> .</p>
  
  <p>I've tried to adapt the code in the link <a href="https://docs.unity3d.com/ScriptReference/Mesh.CombineMeshes.html" rel="nofollow noreferrer"><code>CombineMeshes()</code></a>, so here's my full script which is very simple: it instanciates + tries to merge everything at the end:</p>
  
  <pre><code>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 &lt;= 16; z++) {
              for (int x = -14; x &lt;= 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&lt;MeshFilter&gt;();
                      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 &lt; 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&lt;MeshFilter&gt;().mesh = new Mesh();
          transform.GetComponent&lt;MeshFilter&gt;().mesh.CombineMeshes(combine);
      }
  }
  </code></pre>
  
  <p>When I call <code>CombineMeshes()</code> I get 431 times the error:</p>
  
  <pre><code>Cannot combine mesh that does not allow access: Cube
  UnityEngine.Mesh:CombineMeshes(CombineInstance[])
  </code></pre>
  
  <p>What am I doing wrong?</p>