Do I NEED to handle the MissingReferenceException?

Hi everyone,

I’ve been using Unity for a while now and was curious if I needed to handle the MissingReferenceException any time one pops up. This is occurring while the bullets are flying towards a target and the target disappears (because it has been destroyed). This doesn’t crash the game or give any abnormal behavior, (and in fact I like the behavior it takes once they are destroyed). Will this cause any actual harm in the long run? Everything seems to work fine, but I would hate to add some more work onto this and have it bite me later.

Thank you!

Short answer is yes, you do. You are determining things in code that are unecessary and furthermore you are logging things as errors which in itself also requires computation. Im sure the profiler (ctrl + 7) will also show you this.

@RobAnthem is correct in stating that a simple check for target before firing should suffice:

// When firing the shot
if(_myTarget != null)
      // fire the shot

// once the shot it fired it should not be doing anything else in update so if the target dies midair it should'nt care.

However more detail on how you are handling shooting would help know how to null check or ensure that a reference is correct.