In this code example, the variable called myLight is a Light which should be set in the Inspector window. If this variable is not set, then it will default to null. Attempting to change the color of the light in the try block causes a NullReferenceException which is picked up by the catch block. The catch block displays a message which might be more helpful to artists and game designers, and reminds them to set the light in the inspector.
Summary
NullReferenceException happens when your script code tries to use a variable which isn’t set (referencing) and object.
The error message that appears tells you a great deal about where in the code the problem happens.
NullReferenceException can be avoided by writing code that checks for null before accessing an object, or uses try/catch blocks.