how to quit simulation after my script crashes?

Hi,
I got a bug on my script (a null reference) and the editor freeze, I can see the console window, and I can move it but I can-t close it. Is there some way to stop the simulation? There are only Play, Pause and Step menu items and none of them works consistently once I get this point.
What is the recommend workflow when you hit a bug in your script?
I was forced to quit Unity.to get a workable project again.

thanks

Well, there are 2 kinds of bugs, those that will make Unity let out a warning message and halt the game, and those that will make Unity itself freeze.

The former is relatively easy to catch, since the messages will tell you on what line it happened.

The latter, well, they’re much harder to catch, since Unity is frozen and won’t give any feedback…
And the worst part is that the kind of bug that will halt Unity isn’t actually a bug per se, they’re more like logical glitches, that throw the app into an endless loop or something…

Take this bit o’ C# here, for instance:

public bool crashMe { get { return crashMe; } }

This is the quickest way to freeze Unity without raising any compiler errors… not even Visual Studio will pick it up, and it will throw the program into an endless loop as it tries to return a value that only makes it try again.

In your case, you have a null reference bug… those are still in the first category, but they’re one of the more annoying ones, since the mistake is not on the line indicated in the error message…

What you can do with a null reference error is this:

  • Go to the line the error message indicates… that’s where the program halted.
  • Suppose the line is something like this:
    var myVar = someObject.someMember.someMethod(someArgument);
  • Now, suppose that someMember is null because, although it’s been declared, it was never given a value… The type of someMember implies it has a someMethod method, which in theory you can call… but since the object is null, you get a nice big null reference error instead :wink:

See what I mean about those being annoying? This kind of bug will have you checking back up your code, trying to spot a semantical error (not a sintax one)…

The quickest way of getting around that error, is to just check for whether the offending object is null before doing anything with it… but that doesn’t really fix the problem, it just bypasses it… and while this may work, on the long run that null object might creep up again, forcing you to check if it’s null a gazillion times…

The best thing you can do is to really give your object a value… be it by fixing some code where the object should have been assigned a value buit wasn’t, or by just initializing it with a default value. (for example, if it’s a Vector3, start it up as myVector3 = Vector3.zero or something, so it has a value and isn’t null.

Sorry for the essay, I get a little long-winded at times :wink:
Hope this helps

Cheers

Hey HarvesteR. many thanks for this, very kind if your part!:smile: