How to use "is not null" in unity?

Currently it gives error even when using unity 2020.

How to report your problem productively in the Unity3D forums:

http://plbm.com/?p=220

How to understand compiler and other errors and even fix them yourself:

If you post a code snippet, ALWAYS USE CODE TAGS:

How to use code tags: Using code tags properly

I don’t think Unity supports C# 9 yet, which is where the feature you are describing comes from.

Just is thing != null

1 Like

It is actually not equal to !=null

!=null usage may not work in some special cases.

I am currently using “is object” or “!(is null)” for the same effect but “is not null” is much better for readability.

My mistake was i thought it was a C# 8 feature not 9

Also @Kurt-Dekker
How to not copy paste scripted messages:
https://cutt.ly/hxsffyr

I’m going to guess that this “is not null” silliness is going to have the same massive set of caveats that the ? null conditional / null coalescing operator does when applied to any object derived from UnityEngine.Object.

Under certain conditions it WILL work (with a real null), under other conditions (when the object is merely Destroyed but not truly null) it won’t work, etc. UGH! Dirty, dirty, dirty code.

Just compare it to null or realize it as a bool. At least you get consistent behavior.

Python also had some weird “is None” or “is not None” or something and it had a bunch of really subtle distinctions you had to beware of. It baffles me why you would willingly use code that had weird edge rules to remember, when that code gets you absolutely nothing. Bizarre.

1 Like

I’ve been using (obj != null) as @PraetorBlue suggested - it hasn’t failed once. There may be specific exceptions to it working, but I’m not [yet] aware of any.

2 Likes

When Using != with UnityEngine.Objects requires quite a lot of running costs. So i suggest using !(UnityEngine.Object is null) instead

1 Like

Just always use these handy extension methods:

    public static class ObjectExtensions
    {
        /// <summary>
        /// Returns true if the object is null or the unity object is considered as "null" (was destroyed)
        /// </summary>
        public static bool IsNull<T>(this T obj) where T : class
        {
            if (obj is UnityEngine.Object unityObject)
                return unityObject == null;

            return obj == null;
        }

        /// <summary>
        /// Returns true if the object is not null and the unity object is not considered as "null" (not destroyed)
        /// </summary>
        public static bool IsExistent<T>(this T obj) where T : class
        {
            return !IsNull(obj);
        }

        /// <summary>
        /// returns the given object or a real null-pointer if the object is null or considered as "null" (destroyed unity object).
        /// Use this in combination with a coalescing operator.
        /// Example: `MyObject.Get()?.DoSomething();`
        /// </summary>
        public static T Get<T>(this T obj) where T : class
        {
            if (IsNull(obj))
                return null;

            return obj;
        }

        /// <summary>
        /// Invokes the given action with the element as parameter,
        /// if the object is not null / the unity-object is not considered as "null" (destroyed).
        /// Otherwise, nothing happens.
        /// </summary>
        public static void Do<T>(this T obj, Action<T> action) where T : class
        {
            if (IsNull(obj))
                return;

            action?.Invoke(obj);
        }
    }

You are welcome.

2 Likes