When assigning mesh collider, errors about "doesn't have Read/Write enabled"

This issue is occurring in Unity 2021.2.10f1

I’m generating procedural meshes, storing those meshes in Scriptable Objects, and then assigning them to a mesh collider. However, upon upgrading from 2020 → 2021, I started getting these errors where I assign the Mesh Collider:

This Mesh Collider is attached to GameObject at path 'Prefab Mode in Context/Walkway_Small_Corner' with Mesh 'Walkway_Small_Corner_mesh_fc3f22ce-e547-470e-ab87-5ceb032bd183_0' in Scene 'Walkway_Small_Corner' but doesn't have Read/Write enabled. Only colliders that have default cooking options, are not scaled non-uniformly, and their meshes are not marked dirty can leave this disabled.

The stacktrace comes back to something as simple as:

submesh.MeshCollider.sharedMesh = unityMesh;

Why would this error be occurring? How can I set the read/write flag of a mesh? I can’t find anything about that anywhere. The mesh collider is definitely (1,1,1) scale, I am quite sure the cooking options are default, but I guess their meshes could be dirty as they are being updated in the editor and baked procedurally.

1 Like

Hi,

You don’t always need the mesh to be Read/Write Enabled. If it does, tick the box in the Model Import settings.

Thanks arfish, I know how to do it in the importer, but I am more looking for how to do it in code as these are procedurally generated meshes, and to actually understand what the heck it is actually complaining about.

According to the manual a Read/Write Enabled mesh can be flagged false at run time to save memory.

And there are some new documentation for MeshDataArray from 2020 and up.

If that not what’s going on is sounds like it may be a bug to report then.

1 Like

@cowtrix1 having the exact same problem after upgrading to 2021. Did you find a workaround or open a bug report?

I’ve been trying to find a workaround and I learned a few things.
It only happens to me if I try to set meshCollider.sharedMesh to a mesh that has no vertices.

I tried reproducing this in a minimal project, and it only happened if the cooking options are anything other than Everything. I have a much larger project where I’ve verified the cooking options are on Everything, the scale is (1,1,1), and both the sharedMesh and the new Mesh are set to isReadable, and the same problem occurs. It doesn’t occur with every meshCollider with a procedural mesh in that project, but I haven’t yet determined what the difference is when it happens.

Also I was able to verify this happens at runtime, and not just in the editor. Issue still exists in unity 2022

2 Likes

Hey rifewithkaiju, no I hadn’t opened an issue as I didn’t have a good minimal repro, so thanks for looking into that. The zero verts thing is interesting, that’s definitely something to look into for me…

1 Like

For reference @rifewithkaiju , it looks like preventing my scripts from setting a collider with zero verts did end up resolving the issue here. Would be fantastic if this error message was more explanatory of this possibility though.

1 Like

Unity 2021.3.6f1
I was getting this error after upgrade to 2021.
In my case i fixed this by disabling collider on game object.
My project is using procedurally generated terrain. I tried to post some simplified codes to fix this problem below.

I was also getting another error when baking collider Physics.BakeMesh . So overall solution for me was

  1. Create new mesh with name,
  2. Disable collider
  3. Set game object active
  4. Set vertices
  5. Recalculate Normals
  6. Bake mesh collider in thread (if i bake mesh before recalculate mesh collider doesnt work.)
  7. Set Mesh Collider
  8. Enable collider

_ChunkMesh= new Mesh() {
name = “ChunkMesh”+ChunkIndex
};
if(collider!=null){
collider.enabled=false;
}
transform.position = new Vector3(Position.x, 0f, Position.y);
gameObject.SetActive( true );

public void AsyncBakeMesh(){
Physics.BakeMesh(_MeshInstanceId, false);
}

private void SetMeshVertices( )
{
_ChunkMesh.SetVertices(verticesToUpdate);
_ChunkMesh.SetTriangles(trianglesToUpdate,0, false);
_ChunkMesh.SetColors(colorsToUpdate);
_MeshInstanceId = _ChunkMesh.GetInstanceID();
}

private void RecalculateMesh( )
{
_ChunkMesh.RecalculateNormals();
filter.mesh = _ChunkMesh;
}

private void SetMeshCollider( )
{
collider.sharedMesh = _ChunkMesh;
collider.enabled=true;
}

I would also like to know a solution to this.
I am getting spammed by these messages in my procedurally generated world, where meshes are created/updated all the time.

I haven’t tested it but this is apparently fixed in 2022.2.0a12

1 Like

I got this problem when replacing my procedural map chunks with new ones (not zero vertices). Fixed it by setting meshCollider.sharedMesh = null whenever I remove mesh data with mesh.Clear().

1 Like