Skip to content

Instantly share code, notes, and snippets.

@MeikelLP
Last active August 8, 2019 12:11
Show Gist options
  • Save MeikelLP/2af346f6f5cb98ce335f982416732dee to your computer and use it in GitHub Desktop.
Save MeikelLP/2af346f6f5cb98ce335f982416732dee to your computer and use it in GitHub Desktop.
Unity null & is null & == true

The .? operator ignores the overridden == operator of UnityEngine.Object. It is basically a x is null check under the hood.

assuming obj is a UnityEngine.Object

UnityEngine obj;

// declare and set null
obj = null;
Debug.Log(obj == null); // true (slow)
Debug.Log(obj == true); // false (faster)
Debug.Log(obj is null); // true (fastest)

// intialize
obj = new UnityEngine.Object();
Debug.Log(obj == null); // false (slow)
Debug.Log(obj == true); // true (faster)
Debug.Log(obj is true); // true (fastest) - just checks if the C# object is not null (ignores overriden == operator)

// destroy
UnityEngine.Destroy(obj);
Debug.Log(obj == null); // true (slow)
Debug.Log(obj == true); // false (faster)
Debug.Log(obj is null); // might be true (not deterministic) C#7 feature btw

Links

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment