Skip to content

Instantly share code, notes, and snippets.

@SeppahBaws
Created January 13, 2019 13:28
Show Gist options
  • Save SeppahBaws/bca394e159f71bdfd5335613f50b1bc4 to your computer and use it in GitHub Desktop.
Save SeppahBaws/bca394e159f71bdfd5335613f50b1bc4 to your computer and use it in GitHub Desktop.
This is a simple function that makes checking variables for null much easier. It can even log a simple message. (in this case to the Unity debug console)
public static class Utils
{
public static bool NullCheck(this object obj, string msg = "")
{
if (obj == null)
{
if (msg != "")
Debug.LogError(msg);
return true;
}
return false;
}
}
// --- Usage: ---
Transform someObj = transform.Find("SomeChild");
if (someObj.NullCheck("someObj not found!")) return;
// do stuff with someObj
// ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment