Should I always check for null when assigning objects to memory?

I have a script that uses GameObject.Find(“something”) to assign different objectsto GameObject variables but some of the objects are split across different scenes in my game and therefore prompt a NullReferenceException warning. Should I be checking for null on each of these objects before they are assigned or should I create a script for each scene assigning only the gameObjects that are present in each scene?

36161-2134.png

Hi looMeenin!

I guess you should… Each time you are asking to find an object, you are actually searching it in the scene. So when you try to find an “inexistent” object, it will remains null…

This is really easy and only require time from you. And it is always good to prevent these crashes.

if (myObject != null)
{//Do what you wanna do!;}
else
{
    GameObject.Find("myObject");
    //Redo what you wanna do!;
}

Math