Assert.IsNotNull not working for texture

When I use:

Assert.IsNotNull(_texture, "Missing texture.");
Assert.IsFalse(_texture== null, "Missing texture.");

With my “_texture” variable not set in the inspector, the first assert isn’t fired. Only the second one is. So for some reason IsNotNull doesn’t work for textures.

I tried “IsNotNull(null)” and that does work, so it seems to be a problem with textures.

Perhaps this is a bug? Or am I missing something?

Unity overrides the equality operator (==) for objects that derive from UnityEngine.Object. _texture has a reference to a C# object. That reference is real, and if you compare System.Object equality, by casting _texture to a System.Object, you will see that ((System.Object)_texture) == null is false.

However, the c# object that _texture references is a UnityEngine.Object. So _texture == null invokes the overloaded Unity equality check, which returns true because the internal engine counterpart to the c# object doesn’t exist for whatever internal reason.

Unity’s overloaded equality operator is evil.

Ah Yes, I’ve read that before somewhere, but forgot it seems :stuck_out_tongue:

Kind of stupid that the assertion stuff doesn’t use the overridden equality operator…