Search Unity

Is "NullReferenceException: Object reference not set.." always bad even though code works?

Discussion in 'Getting Started' started by Abelabumba, Feb 4, 2015.

  1. Abelabumba

    Abelabumba

    Joined:
    Jan 14, 2015
    Posts:
    45
    I'm talking about where I set up something like

    public GameObject crewToBuy;

    That later gets filled with

    crewToBuy = GameObject.FindWithTag("MarkedCrew");

    In the inspector the crew to buy field will show "None (Game Object)" but the game will start and run without any visible errors. The console will spit out the "NullReferenceException" Error non stop once the game has started though, until the crewToBuy gets associated with something inGame (which is what it was supposed to do, there is no gameplay benefit from putting something in there in the inspector). Should I even care about this kind of "Error that I know will disappear later in game"? Is there something I can/should do about it, like putting in some empty GameObject into the field in the inspector, just to make the console shut up?
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Yes, it's bad. There should never be any errors when running a game. Your code should be checking whether objects are null before accessing them.

    --Eric
     
    rahulk1991 and Jessy like this.
  3. Ramsdal

    Ramsdal

    Joined:
    Oct 18, 2013
    Posts:
    251
    Uninitialized GameObject variables will not cause exceptions if they are not referenced, so somewhere in an update or through gui or something you must be calling something that tries access this object?

    As Eric says, check for null where the exception occurs. :)
     
  4. Abelabumba

    Abelabumba

    Joined:
    Jan 14, 2015
    Posts:
    45
    Thanks for the fast responses. I put in
    Code (csharp):
    1.  
    2. if (crewToBuy != null)
    3.             {        
    4.             myCrewStats = crewToBuy.GetComponent<CrewStats>();
    5.             }
    and error disappeared. If someone has the patience and fun in explaining why this is better than just letting the error run I'd be interested to know why this is better. In my uneducated mind I don't see why constantly checking for null is better than just ignoring the seemingly harmless error - but I'm obviously not doubting you guys.
     
    Last edited by a moderator: Feb 5, 2015
    rahulk1991 likes this.
  5. Ramsdal

    Ramsdal

    Joined:
    Oct 18, 2013
    Posts:
    251
    An error should always be avoided when possible if for no other reason then because it indicates sloppy code on the part of the developer. Also if it pumps out many errors of no consequense, then it is easier to miss a potential relevant one.
    But if there is any heavy performance impact I do not know. However if it is logged somwehere, that would require more effort than just checking for null.
     
    Last edited: Feb 4, 2015
  6. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Errors can cause the build to crash. The Unity editor will handle it so you can continue working, but you should turn on "error pause" in the console rather than letting it continue to run with errors.

    --Eric
     
  7. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Errors are slow.

    Errors can cause your game to behave in unexpected ways.

    Errors should never be part of the normal code flow.
     
    rahulk1991 and Jessy like this.