RaycastHit.textureCoord returns zero on Android if mesh is not set to readable

Hello, we are using RaycastHit.textureCoord to find the UV coordinates of a car mesh collider in order to apply a decal on the car body.

The mesh of the mesh collider is non convex and is taken directly from the car model.
Everything works correctly in the editor, but its not in Android build.

Specifically, RaycastHit.textureCoord always returns zero in the Android build unless the mesh of the mesh collider is set to readable.

Is there any way to avoid this? Enabling Read / write means to double memory occupation.

So you want to read mesh information without having the mesh data actually available in memory? How do you think this should work? If you just upload the mesh data to the GPU memory you can not do any raycasting against the collider and query mesh relevant data (like the UV coordinates). If you need to query texture coordinates from the mesh it has to be readable. There’s no way around that.

How large is your mesh that you worry about having it in RAM? Mesh data, even for moderate complex meshes, shouldn’t be an issue compared to texture or audio data.

This is something that its not mentioned on RaycastHit.textureCoord documentation. It only says “Note: A textureCoord requires the collider to be a MeshCollider”.

For what concern enabling read write checkbox, even Unity documentation pushes to avoid this whenever is possible, even by specifying when it should be enabled (Unity - Scripting API: Mesh.isReadable):

In most cases, you should disable this option to save runtime memory usage. You should only enable it under the following circumstances:

  • When you read from or write to the Mesh data in your code.
  • When you pass the Mesh to StaticBatchingUtility.Combine() to combine the Mesh at run time.
    -When you pass the mesh to CanvasRenderer.SetMesh.
  • When you use the Mesh to bake a NavMesh using the NavMesh building components at run time.
  • When the Mesh is convex, you use the Mesh with a Mesh Collider, and the Mesh Collider’s Transform has negative scaling (for example, (–1, 1, 1)).
  • When you use the Mesh with a Mesh Collider, and the Mesh Collider’s transform is skewed or sheared (for example, when a rotated Transform has a scaled parent Transform).
  • When you use the Mesh with a Mesh Collider, and the Mesh Collider’s Cooking Options flags are set to any value other than the default.
    -When using a Mesh with a Particle System’s Shape module or Renderer module when using not using GPU instancing. Note that the Particle System will automatically change Meshes to readable when assigned through the inspector