Null freezing issue (C#)

Hi,

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.

Please tell me this is gone in Unity3!!?

What would you expect it to output? You didn’t pass it an argument if the value doesn’t exist.

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.

What code do you have wrapping that Debug.Log call? If it throws an unhandled exception, it may be your code causing an infinite loop or something.

So far I have yet to have an issue with Unity freezing from unhandled exceptions.

Nothing wrapping it. I’ll try and post a repro asap.

OK, this causes a freeze every time I run it:

        Debug.Log("Start Test");
        Transform xform = transform;
        Debug.Log(xform.name);
        xform = null;
        Debug.Log(xform);       // OK to here.
        Debug.Log(xform.name);  // Freeze!

Take out the last Debug.Log line and it works as expected.

  • Rafe

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.

Try downloading the project I created here:
http://genexissystems.com/NullReferenceCrash.zip

Let me know if that hangs for you too.

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?

Cheers

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.

.

Windows XP, 32bit.