Why Unity Crashes on a compiled Script? (Server - Client)

Hello the question im asking is, "what are the common reasons why unity crashes? where do i stat to look for a sollution?" as i have a script that complies fine without errors, also as Server ingame works fine, but once a Client connects unity crashes, Ive snipt at the source code of the script (usin java) and narrowed it down to calling in the UPDATE():

var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
enemyTrans = MarioController.enemySelected.transform;

Once i remove these two from the source the clients connect fine and the game continues. But i would like to know the asnwers to my question abouve, as its essential that i use raycast in this script as all my other functions depend on it.



Regarding your "main question" ("what are the common reasons why Unity crashes? Where do I start to look for a solution"), the answer would be: Infinite recursions and loops and the like. In my experience, that's the single most frequent reason why Unity crashes.

I've also had issues probably related to my gfx-card a while back (when doing super-hi-res screenshots ... that not only crashed Unity but actually put my whole Mac to a halt when it happened). With such issues, it's great if you have multiple machines to test on (ideally even on different platforms).

Regarding your specific question: You already seem to have narrowed it down, which is what you'd usually try to do: First try to find a way to reliably reproduce the crash, then simplify the issue until the crash disappears, then try to make the crash re-appear. If it's a problem in your code (e.g. an infinite loop or recursion), that'll help you fix your bug - if it's a problem with Unity, it's a good foundation for writing a bug report that actually helps the UT QA and dev teams to fix the issue.

From the code you've posted, it doesn't seem likely to me that this really is the issue. It looks more like commenting these lines out would have a side-effect somewhere else that prevents the crash from happening (e.g. ray or enemyTrans not being available later and then some code causing the crash not being executed because of a null-check).

What kind of surprises me is that you don't get a compilation error when commenting out "var ray = ..."; that should mean that "ray" is never used, so that whole line is dead code that you should probably remove anyways.

Does the crash only disappear when you remove both lines? Does any functionality break when you remove those lines? If functionality breaks, look in there for what's really causing the crash ...

Ah, one final note: In general, compilation errors are much easier to find and fix than runtime errors. Crashes are always runtime errors that usually don't really have anything to do with compilation errors. When you get a compilation error, any changes you may have applied won't be available during runtime (that might be confusing when you make changes because they likely don't have any effect; always first fix any compilation errors before even play-testing your game).

When Unity crashes, the first place I look is the editor logfile, to see if it contains any clue. If that doesn't work, I try to isolate the problem. Remove half of my code/logic, and see if it's still there. repeat untill it's gone. when you find out a subset of code/logic that's causing the problem, zoom in on that, by keep cutting away more.

When you say Crash, do you mean that the Unity Editor program terminates, and it starts over, or do you mean that the game stops playing?

Thanks alot for your time m8 yeh it seems there reasons are endless but I managed to find out what was causing the editor to crash,it was that the instantiated network client prefab had child gameobject that uses the same script as the server instanciated network player, some scripts need To be inactive! In my case raycast and selected enemy with the related functions needed to be pit on a seperate script so i can disable it if netwrok view is not mine in the awake() function and Wala! It's works fine and doesn't crash the editor anymore. I should have known any script that has caracter controller calls in it should be inactive for remote instantiated players in your scene. Thanks again.