Search Unity

Resolved Getting a null reference exception upon checking the tag of a Raycasthit2d

Discussion in 'Scripting' started by Chazzwazzler, Jul 25, 2021.

  1. Chazzwazzler

    Chazzwazzler

    Joined:
    May 2, 2018
    Posts:
    35
    Here is my code:

    Code (CSharp):
    1.             RaycastHit2D hit = Physics2D.Linecast(transform.position, player.transform.position, detectionMask);
    2.  
    3.             if (hit.collider.tag == "Player")
    4.             {
    5.                 //shoot and stuff
    6.             }
    The problem is, that when I run it, I get this error no matter what:
    upload_2021-7-25_1-39-16.png
    (The error corresponds to the line where I check the tag)

    I have already tried a lot of debugging things to find out the problem, but none have worked. I have checked if the Raycasthit2D is null, I have checked if its tag is null, I have had it debug out the tag of the raycasthit2d and it said the correct tag, I have tried switching it to comparetag(), all to no avail.

    What am I missing here? I have no idea and none of the solutions I have found online (which is the things that I said I tried earlier) have worked yet.
     
    Last edited: Jul 25, 2021
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    As the example here suggests:
    https://docs.unity3d.com/ScriptReference/RaycastHit2D-collider.html
    I'm pretty sure while hit wont be null, if you simply dont hit anything, the hit.collider will be null. You then continue, regardless, with hit.collider.tag, which may be null.tag, throwing the exception.

    Also, just for reassurance in the future it may be a good idea, if you only post a fraction of the code, to say that the line mentioned in the error corresponds to line X in the code sniplet posted.
     
    Bunny83 likes this.
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,491
    As stated above, you're just assuming you always hit and proceed to check the collider you hit. Stop and think; what collider would it return if it didn't hit a collider? Obviously none so it returns NULL but you then try to effectively do "if (hit.NULL.tag == "Player")" hence a NullReferenceException.

    Either check if the Collider is NULL or use the much more syntax friendly operator I added to it like this:

    Code (CSharp):
    1. RaycastHit2D hit = Physics2D.Linecast(transform.position, player.transform.position, detectionMask);
    2. if (hit && hit.collider.tag == "Player")
    3. {
    4.     //shoot and stuff
    5. }
    In short, you can do "if (hit)" which will check for you to see if it's a valid hit or not. In the code above I did it before accessing any of the content of the RaycastHit2D.
     
    Bunny83 likes this.
  4. Chazzwazzler

    Chazzwazzler

    Joined:
    May 2, 2018
    Posts:
    35
    None of it is null though. I have been having it log in the console the name of the object it is hitting, its position, and its tag, and none of it is null and always returns correct. Just for some reason no matter what the if statement checking the tag always returns a null reference exception error, even though all my checks are saying none of the stuff that goes into checking the tag is null.
     
  5. VolodymyrBS

    VolodymyrBS

    Joined:
    May 15, 2019
    Posts:
    150
    There is no magic. If it throw NullReferenceException than something is definitely is null.

    logs is good but best tool is debugger.
    All you need:
    1. start debugger (concrete instruction depend on what IDE you are using)
    2. set breakpoint on line where exception is thrown.
    3. star game and wait when breakpoint hit.
    4. Check every variable and field you access in that line to find what is null.
    5. fix it.
     
  6. Chazzwazzler

    Chazzwazzler

    Joined:
    May 2, 2018
    Posts:
    35
    Well I was able to fix it, I think either visual studio or unity was broken or something for a little bit. I deleted the code causing the error, saved the script, restarted unity, added the code back in, and then the error stopped and my code worked.