Search Unity

help i have been trying to fix this error for ages and ive gotten nowhere

Discussion in 'Scripting' started by texdavis, Jun 25, 2019.

  1. texdavis

    texdavis

    Joined:
    May 3, 2019
    Posts:
    3
    NullReferenceException: Object reference not set to an instance of an object
    GameMaster.KillPlayer (Player player) (at Assets/GameMaster.cs:32)
    Player.DamagePlayer (System.Int32 damage) (at Assets/Player.cs:19)
    Player.Update () (at Assets/Player.cs:13)

    i've been following a tutorial because this is my first time coding.
     
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    When asking for help debugging an error, you should usually post the code you are editing. Use code tags, and remember to clearly indicate which line is getting the error.

    In general terms, a NullReferenceException means that you are assuming your variable has a value but it actually does not. This can occur if you never assigned it a value, or if you assigned it to an object that has since been destroyed, or if you specifically set it to null.

    "Assuming your variable has a value" basically means any time you put a period after the variable name. Also certain special cases like trying to iterate over it with a foreach loop.

    If you are following a tutorial, you may have made a typo somewhere. Remember that Capitalization matters.

    Also you might have forgotten to assign a reference to the variable in the inspector. This can be tricky to spot because your code is exactly right but it's counting on some set-up that you haven't done.