Friday, October 9, 2015

Unity 3D: Compiled game freezes or crashes on exit

Sometimes -- and it seems to happen much more often when dealing with blocking resources (such as serial ports) or multiple threads -- your built Unity game will crash or hang when calling Application.Quit ().

First and foremost, check your logs and make sure you're not throwing an exception as the program exits. And of course check your code to make sure you haven't inadvertently made an infinite loop somewhere. Clean up any resources that may not be handled automatically -- for instance, close serial ports, kill threads, etc.

Assuming you've done all that and you're still freezing when the program exits, try replacing Application.Quit () with the following lines of code:

if (!Application.isEditor)
{
     System.Diagnostics.Process.GetCurrentProcess().Kill();
}

There seems to be the occasional bug that rears its head with Application.Quit (), so we're using a .NET command to kill the process directly. We are also checking to make sure that we're not in the Editor, because if you run this line of code from the Editor, it will close.

Tuesday, July 21, 2015

Unity 3D: Console displays wrong folder path for files

Sometimes, especially if you're bouncing projects between different users, your Console may show that your source files are in a different folder location than where they're actually stored.

For instance, let's say you have a null reference error in a "DoStuff ()" method. Normally, the second line on the console will show you the method name, and if you double-click the error, it will take you to that line of code. However, if you see a path to the source file and it's not valid (say, a path on someone else's computer), then it won't open MonoDevelop and you've got a problem.

There is, fortunately, a simple fix. Close the project, go to the project's directory, and find the "Library" folder. Delete everything inside of it, and open your project again. Unity will have to re-build the entire asset library, so it will take longer than normal to open, but once it does, your issue should be resolved. If it isn't, re-sync your MonoDevelop project ("Assets > Sync MonoDevelop Project").

If you have a team working on a project together, shipping the whole project back and forth is a bad way to work for a number of reasons. Look into version control!

Thursday, July 16, 2015

Unity 3D: "Screen position out of view frustum" error

"Screen position out of view frustum" is an error almost everyone will see at some point. This can be caused by a few different things, and fortunately is pretty easily fixed.

First off, there is a bug (at least as of Unity 4.5) that can cause this to happen randomly. The quickest way to find out if that's your issue is to close the "Scene" tab and reopen it from the "Window" menu. If there's nothing in your scene causing this issue, it should now go away.

If you're using an orthographic camera, don't set the orthographicSize to 0 or this will happen. Don't forget to check not only the editor, but any scripts attached to the camera -- any zooming scripts you have may be setting it 0 as well.

The last issue I've seen that causes this error is having a camera with a near clip plane set to 0. Again, don't just check the editor; make sure you look at any scripts manipulating the camera.

If anyone finds any other issues that can cause this error, let me know and I'll add it!