Back to Writing
NOTESunitycsharpdebuggingbest-practices

Unity — Using Debug.Assert for Cleaner Null Checks

May 22, 2021Updated Feb 17, 2026

![](

MMhpxkvSfcu9HPFlcYg.4mnReSroHF6n8TNKNfykRTgSbfdBiY4nl_NUHvEECGMg.PNG.cdw0424/image.png?type=w966)

In Unity, we use the Inspector window all the time.

And to prevent mistakes, I used to write null checks everywhere like if(ip is null)~~~ every single time.

Later I switched to using the null-coalescing operator like "??=" to handle it.

But even that code was getting too long.

![](

9XUClcDUMJ_a0ChI6wg.M9WKV3ZrA7Mr2EAXAIbVbYbXN6yuzPQLVwg3BZv4GCEg.PNG.cdw0424/image.png?type=w966)

So I started using Assert.

(If the argument is false, it displays an error message and call stack)

![](

V0cNCBnXsOZ-6RLfHEg.WiACeN4awYj3VNNT2rE6RmjBOcBKzE5Df4itCutaJpUg.PNG.cdw0424/image.png?type=w966)

It turned out to be way more convenient than I expected, so I wanted to document it.

I actually knew Unity supported Assert, but I got so fixated on null handling that I forgot about it. Once I tried it, it was incredibly convenient.

Why did it take me this long to figure this out...