Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question NeoFPS HitscanShooter null pointer effect hit

Discussion in 'Scripting' started by Aseliot, Dec 8, 2022.

  1. Aseliot

    Aseliot

    Joined:
    May 7, 2019
    Posts:
    35
    So this is weird but I have a null pointer error that I cannot find because it doesn't appear to occur when using the debugger or when debug logging with if statements or anything.

    It is in
    NeoFPS\Core\Weapons\ModularFirearm\Shooters\HitscanShooter.cs
    line 151
    Code (CSharp):
    1. effect.Hit(m_Hit, newRayDirection, m_Hit.distance, float.PositiveInfinity, firearm as IDamageSource);
    and/or 157
    Code (CSharp):
    1. effect.Hit(m_Hit, ray.direction, m_Hit.distance, float.PositiveInfinity, firearm as IDamageSource);
    I have checked the variables effect and all parameters passed multiple times now but none of them are null. Debug logging them never prints null and neither with a breakpoint. I am even able to break in the Hit function.

    I am also able to get into the Hit function being executed and nothing appears to be null there either except for maybe collider, but commenting out the code doesn't help. I already commented out the whole function contents and it keeps throwing the errors at lines 151/157

    upload_2022-12-8_12-13-59.png

    Even put a catch around it

    Code (CSharp):
    1. catch (System.Exception e)
    2.             {
    3.                 Debug.Log(effect);
    4.                 Debug.Log(hitPoint);
    5.                 Debug.Log(firearm);
    6.             }
    But it logs them just fine? huh?

    upload_2022-12-8_12-22-14.png

    Any other ways I could figure out what is going on because it appears as if nothing is null and there should not be an exception.

    I can't really see any way to break on exception (as you could with PHP for example)
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,883
    If it's happening on that line, would that imply that
    effect
    is what's null?
     
  3. Aseliot

    Aseliot

    Joined:
    May 7, 2019
    Posts:
    35
    Well you can see I logged it as the BulletAmmoEffect object at the top of the console in the image.

    Only thing I can think of that something inside of the objects might be null. But then the error reporting on this line would be weird?
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,883
    The only parameter that can be null is your IDamageSource interface, which could potentially happen if you cast an invalid type (using
    as
    casting results in a
    null
    if you try an invalid cast).

    However, if one of the parameters happened to be null, the error would be pointing to the line where you try to access any of the null value's properties. The error is pointing to the method call itself.

    The only property you are accessing in the method call itself is
    m_hit.distance
    , which is a struct and can't be null.

    Thus only
    effect
    itself can be the source of the null reference error. So looking into the method is taking you on a wild goose chase and you probably want to look into why
    effect
    is null.
     
  5. Aseliot

    Aseliot

    Joined:
    May 7, 2019
    Posts:
    35
    Ah nvm it was another script that was called through an event listener, not sure why the error wouldn't just be displayed on that line when clicking it.

    I noticed the error in the console (I didn't show it here) had another script name in it than the script it showed me when clicking on it.