Search Unity

"Object reference not set to an instance of an object [...]"

Discussion in 'Scripting' started by kevinbaranczyk, Dec 2, 2017.

  1. kevinbaranczyk

    kevinbaranczyk

    Joined:
    Nov 20, 2017
    Posts:
    2
    Hi there Guys,

    I'm a gamedev student, Unity begginer and one frustrated person.
    Why?
    I have started Asbjørn Thirslund's Brackeys Unity tutorial on YouTube, been doing as the guy says, but still I've encountered an issue, which, for 4 days, I've been trying to solve myself and failed.
    I talk about this bloody object reference, as I have seen, many of us struggle to solve. Unfortunately, answers included in other threads don't seem to fit in my solution box.

    What Unity's log says:

    "NullReferenceException: Object reference not set to an instance of an object
    Weapon.Shoot () (at Assets/Scripts/Weapon.cs:56)
    Weapon.Update () (at Assets/Scripts/Weapon.cs:43)"

    How it looks in MonoDevelop:

    upload_2017-12-2_19-22-55.png

    What I say:

    "End my misery."

    I am unbelievably grateful from this very moment to any hero that will answer my cry for help.

    Cheers,
    Kevin
     
  2. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    According to the error message, it's likely that the firepoint variable has not been assigned (i.e. it is null). That's the only thing in that line that would be able to cause the exception.

    Make sure you assign an instance (apparently some Transform) to that variable, either via inspector or via code.
     
    kevinbaranczyk likes this.
  3. Hikiko66

    Hikiko66

    Joined:
    May 5, 2013
    Posts:
    1,304
    It's a really common error, that usually relates to a different line of code that has a bug.
    You need to find out which object is null on the line throwing the error, then you can work out why.

    You need to do yourself a favour and learn the basics of debugging, else finding errors will feel like a guessing game.
    I'm sure there are unity tutorials on this, but this should really be one of the first tutorials pushed at beginners.

    Set a breakpoint on that line and then mouse over or query the objects on that line to see their values. Find the one that is null. Then find out why it is null and why it hasn't been set based on the references to it in the rest of the code. This might also require stepping through the code line by line to check the programme flow to see whether you are hitting the code that is supposed to be populating it.


    Basics:
    -Open up your script in monodev
    -You see the margin on the left of the line numbers of your code? Click it next to the line with the error. You will see a red circle appear. That's a breakpoint. It will pause the programme on that line. Set as many of these as you need.
    -In order to get the programme to pause on breakpoints, you need to attach monodev to the unity instance. In monodev click Run->Attach To Process-> Select Unity Editor and then click attach.
    -Now press play in unity.
    -When that line of code is hit, the programme will pause. You can go to monodev and in yours script hover your mouse over variables to check their values. See which one is null.
    -You can also use the immediate window next to the call stack on the bottom right. That is a console. You can query things there, or even make assignments. For example: typing in "?Var1" will ask it the current value of Var1.
    -You can step through your code line by line if you need to. Pressing F10 will run the current line of code and then stop at the next line of code. This helps especially when you are looking at the flow of the code or checking more abiguous issues that might not even throw an error.
    -You can also make use of the watch on the bottom left. This allows you to keep a watch on variables, at various stages.
    -Call stack is basically a way of checking what function called a different function, and you can follow the programme flow on a higher level.
    -You can also set conditional breakpoints. Right click a breakpoint, click breakpoint properties and setup a condition, like breaking when a variable is a certain value. This is helpful when you want to stop on a line but only on certain conditions, because a problem only occurs under those conditions.
     
    Last edited: Dec 2, 2017
    kevinbaranczyk and Suddoha like this.
  4. kevinbaranczyk

    kevinbaranczyk

    Joined:
    Nov 20, 2017
    Posts:
    2
    First of all, I'd like to thank both of you for your interest. Especially you @NA-RA-KU.

    The thing is, I do not know which value should be incresed to exceed null.

    Here's the whole code and 2 screenshots:

    (thanks again!)
     

    Attached Files:

  5. Hikiko66

    Hikiko66

    Joined:
    May 5, 2013
    Posts:
    1,304
    You should be looking at the firePoint object, not firePointPosition

    I notice you have a null check in awake for firePoint
    Code (csharp):
    1.  
    2.         firePoint = transform.Find ("firePoint");
    3.         if (firePoint == null) {
    4.             Debug.LogError ("No firePoint? WHAT?!");
    5.         }
    6.  
    Does the debug log at the bottom of unity with the errors spit out the phrase: "No firePoint? WHAT?!" ?

    If yes, then your script is attached to an object that can't find firePoint.

    If not, then it might still be null later on, because an object has been destroyed or a reference has been lost in some other way.

    Modify the breakpoint on that line, right click breakpoint and go to properties.
    Set the following condition
    firePoint == null
    Then run the program.

    If it hits that breakpoint but doesn't spit out "No firePoint? WHAT?!" in the unity debug log, then it finds firePoint to start with, but loses that reference later on.
     
    Last edited: Dec 4, 2017