Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

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

Discussion in 'Physics' started by cowtrix, Mar 4, 2022.

  1. cowtrix

    cowtrix

    Joined:
    Oct 23, 2012
    Posts:
    325
    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:

    Code (CSharp):
    1. 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:

    Code (CSharp):
    1. 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.
     
    rifewithkaiju likes this.
  2. arfish

    arfish

    Joined:
    Jan 28, 2017
    Posts:
    787
  3. cowtrix

    cowtrix

    Joined:
    Oct 23, 2012
    Posts:
    325
    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.
     
  4. arfish

    arfish

    Joined:
    Jan 28, 2017
    Posts:
    787
    cowtrix likes this.
  5. rifewithkaiju

    rifewithkaiju

    Joined:
    Mar 8, 2022
    Posts:
    3
    @cowtrix having the exact same problem after upgrading to 2021. Did you find a workaround or open a bug report?
     
  6. rifewithkaiju

    rifewithkaiju

    Joined:
    Mar 8, 2022
    Posts:
    3
    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
     
    Last edited: Mar 8, 2022
    defaxer and cowtrix like this.
  7. cowtrix

    cowtrix

    Joined:
    Oct 23, 2012
    Posts:
    325
    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...
     
    rifewithkaiju likes this.
  8. cowtrix

    cowtrix

    Joined:
    Oct 23, 2012
    Posts:
    325
    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.
     
    rifewithkaiju likes this.
  9. rifewithkaiju

    rifewithkaiju

    Joined:
    Mar 8, 2022
    Posts:
    3
  10. ersinceylan

    ersinceylan

    Joined:
    Jan 13, 2022
    Posts:
    4
    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;
    }
     
  11. deffan

    deffan

    Joined:
    May 24, 2015
    Posts:
    7
    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.
     
  12. cowtrix

    cowtrix

    Joined:
    Oct 23, 2012
    Posts:
    325
    I haven't tested it but this is apparently fixed in 2022.2.0a12
     
    laurentlavigne likes this.
  13. MinoanStudios

    MinoanStudios

    Joined:
    Dec 10, 2017
    Posts:
    11
    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().
     
    jeroen_m likes this.