Issue with Accessing MeshCollider's sharedMesh in Start Function, But Works Fine in OnEnable Function

Hello everyone,

I’ve encountered a peculiar issue and am seeking some assistance. I have a script where I attempt to access the sharedMesh of a MeshCollider in the Start function. However, accessing properties of sharedMesh such as vertices and triangles during this time results in an error in the Unity console.

Specifically, when I try to access sharedMesh.vertices or sharedMesh.triangles in the Start function, Unity outputs an error message. Strangely, the same access operations work flawlessly when placed in the OnEnable function.

I have confirmed through Debug.Log statements that sharedMesh.isReadable is false during the Start function and OnEnable function, indicating that sharedMesh is indeed not readable at that point.

Here is my code:


using UnityEngine;

public class MeshColliderTest : MonoBehaviour
{
    void Start()
    {
        Debug.Log("[MeshColliderTest] Start()");
        MeshCollider meshCollider = GetComponent<MeshCollider>();
        if (meshCollider != null)
        {
            Mesh sharedMesh = meshCollider.sharedMesh;
            Debug.Log("[Start] sharedMesh.isReadable: " + sharedMesh.isReadable);
            Vector3[] vertices = sharedMesh.vertices;
            Debug.Log("[Start] v " + vertices[0].x + " " + vertices[0].y + " " + vertices[0].z);
        }
    }

    void OnEnable()
    {
        Debug.Log("[MeshColliderTest] OnEnable()");
        MeshCollider meshCollider = GetComponent<MeshCollider>();
        if (meshCollider != null)
        {
            Mesh sharedMesh = meshCollider.sharedMesh;
            Debug.Log("[OnEnable] sharedMesh.isReadable: " + sharedMesh.isReadable);
            Vector3[] vertices = sharedMesh.vertices;
            Debug.Log("[OnEnable] v " + vertices[0].x + " " + vertices[0].y + " " + vertices[0].z);
        }
    }
}

This phenomenon is perplexing, as all components should be initialized by the time Start is called. Has anyone encountered a similar issue, or does anyone have any suggestions or solutions?

Thank you very much!

If this mesh has Read/Write ☐ then Editor should throw you this error in both cases. Fact that it is not doing that might mean there is a bug here possibly. Or that mesh is not unloaded from RAM at OnEnable yet but it would be unnecessarily risky to bet on this without testing on all target platforms as this could just be a race condition (semi random).