🤔

NullReferenceException: UnityEngine.Object#

If you want to handle the case where the object is unassigned:

Check the reference isn't null before trying to access it#

  • Exit early if null.
    if (example == null)
    {
        // Exit early.
        return;
    }
    // Code that uses example.
  • Nest your code in a null check.
    if (example != null)
    {
        // Code that uses example.
    }
warning

Do not use modern null-checking operators1 (?., ??, ??=) to check for null.

If you want to correctly assign the object:

  1. See Unity null to learn about the specifics surrounding null and UnityEngine.Object types.