- Home /
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?
As a test, I disabled all code in "Do things here..." and removed all scripts from the object being hit by the raycast. Still no luck.
Answer by Goron40 · Apr 15, 2012 at 09:41 PM
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.
There were lots of bugs and other problems reported with 64 bit versions in the past - it seems these issues have not been fixed yet...
Answer by aldonaletto · Apr 15, 2012 at 07:43 PM
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 took out all the code in the if loop to check this. No luck there.
All scripts have been removed from those meshes to test that theory. Still crashing.
Well, it smells like a bug. How the crashing meshes were created? In some 3D editor? $$anonymous$$aybe some weird data in the model is causing the problem. To test this, you could get some Unity primitive (a cylinder, for instance) and replace the original collider with a mesh collider (Component->Physics->$$anonymous$$esh Collider - remember to set Convex). If this "native" mesh collider still causes the crash, you may have a defective Unity - reinstall it. If it doesn't crash Unity, your models may have some weird characteristic that's activating a Unity's bug - maybe saving the models in a different format and reimporting them could solve the problem.
I tried a primitive collider. No problems.
However, just discovered setting the build to 32bit avoids the crash.
Answer by Sphax84 · Nov 28, 2016 at 12:30 AM
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... :(
Your answer
![](https://koobas.hobune.stream/wayback/20220613070350im_/https://answers.unity.com/themes/thub/images/avi.jpg)