Skip to content

Instantly share code, notes, and snippets.

@thebeardphantom
Last active October 12, 2021 19:07
Show Gist options
  • Save thebeardphantom/007ed806398a8eda0b4ce99681dae3b0 to your computer and use it in GitHub Desktop.
Save thebeardphantom/007ed806398a8eda0b4ce99681dae3b0 to your computer and use it in GitHub Desktop.
Useful if you don't know the type of an object and you want to ensure you don't bypass Unity's native-side null checks.
public static bool IsNull<T>(this T obj) where T : class
{
if(obj is UnityEngine.Object unityObj)
{
/* Invokes Unity's custom == check for nulls.
* This case technically only works because obj is
* a "fake null" on the C# side by this point.
* If its a true null it'd hit the else case instead,
* which fortunately still returns what we want.
*/
return unityObj == null;
}
else
{
// Uses regular C# null check
return obj == null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment