Raycast crashing build on collision with mesh collider

Alright, so I’ve got a segment of code here…

// Get the ray going through the "center" of the screen
var ray:Ray = Camera.main.ScreenPointToRay(Vector3(Screen.width / 2, Screen.height * .6 , 0));

// Do a raycast
var hit : RaycastHit;
if (Physics.Raycast (ray, hit))
{
     Do things here...
}

I’ve tested everything in and around this code and I’m 100% certain that the problem is with this line here:

if (Physics.Raycast (ray, hit))

Now here’s where it gets weird. In the editor, I have no problems and can run this code fine. However, if I build an exe of the project, it will crash as soon as I look at something with a mesh collider. Using primitives will not cause a crash.

The mesh colliders are checked as convex, and although they are over 255 triangles, Unity’s clearly running a simplification algorithm (I can see the simplified mesh in the editor). I have also tried making my own >255 tri mesh and replacing the simplified collider with it, to the same effect.

Does anyone have any idea what’s going on here?

Alright, I “fixed” it. Built a 32 bit build instead of a 64 bit one. No idea why that did anything, I’m on a 64 bit computer. Very strange.

Weird thing! Unless some bug in your Unity version is causing this, there’s nothing in your code to make it crash.

A shot in the dark: the most frequent cause of crashes in Unity is unintentional recursive loops. For instance: if the code inside if (Physics.Raycast(…)) calls a function in the hit object, and this function somehow calls the original code that does the raycast, an infinite recursive loop starts and quickly crashes Unity.

Are you calling some code in these meshes? Or even meshes without any script cause the crash? The crash still happens if you omit the code inside the if (Physics.Raycast(…))?

I get a crash on “Physics.Raycast (ray, hit)” as well on x64 (not tested on x86) when many raycasts are achieved in a loop. This happens in editor while playing or in standalone exactly the same.

I’m 100% sure my code is correct because it does not crash when few raycasts are done. By the way, even if my code wasn’t ok, Unity editor or standalone should never crash…

I already reported it to Unity but they never wanted to investigate to fix that bug… :frowning: