I was working on a small piece of my game when Unity started to freeze. I just spent 4 hours breaking my code in to ever smaller pieces, trying to find the cause, until I was down to a single Debug.Log() line:
Debug.Log(xform.name);
It finally occurred to me that when ‘xform’ is null, it freezes Unity… not a crash, a total complete freeze.
This makes me angry, and at the same time I’m just happy it’s over and I can get back to work.
I have a function that returns a transform or null.
If I Debug.Log(xform.name), and xform is null, I would expect an error. Something like “Null object does not have the property ‘name’.”…or whatever is standard wording for C#.
I do not expect it to freeze Unity so I have to kill the process, and repeat 50 or 60 times until I find the cause.
I created an empty Unity project (version 2.6.1), Added an empty game object, created a new C# script, attached the script to the game object and pasted your code into its update method.
Every frame I get the NullReferenceException (as expected), but it doesn’t cause a hang. It doesn’t hang in the webplayer either.
I’ll download your project asap (on my iPhone ATM).
I forgot to post my details. I’m on windows 7, and I think I have the latest unity but I’ll have to check. What setup were you running when you made the test project?
I’ve seen null references freeze and crash Unity more times than I care to count. I wish I had a good workaround to suggest, but I don’t. I guess we just have to be more diligent about checking for null until debugging support is released.