Catching empty variable

I have a variable grabbing a script from a gameobject when it is selected. When this object dies it gets destroyed. I then get spammed with the error “NullReferenceException: Object reference not set to an instance of an object” until a new gameobject is selected to take its place. How can I catch this so I don’t get spammed in the console?

Check for objects being null in your code, i.e.

if (myObject != null) {
    // do something with myObject
}

–Eric

1 Like