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.

Question Null reference in UnityEngine.Rigidbody.AddForce_Injected ?

Discussion in 'Physics' started by dgoyette, Nov 27, 2022.

  1. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,003
    I had a crash in production today with a strange stack trace. Ultimately, the exception occurred in
    UnityEngine.Rigidbody.AddForce_Injected
    , which I'm not aware I have any control over:

    Since this occurred in production, I don't have the specific line numbers in the stack trace. Though I'm not sure I would have had proper line numbers anyway even in a Debug build, since the exception occurred within Unity's code code. This doesn't appear to be an issue with the rigidbody itself being null, but something else going on. (It's very possible the object being affected was destroyed recently, as rigidbodies often get destroyed in my game.)

    This was a one-off thing. I've never seen it before. So, I can't post a bug report with steps to reproduce it. But I figured I'd mention it in case it has any use. This was in Unity 2021.3 LTS,
    2021.3.7f1.
     
  2. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,003
    No real update on this, but a hunch: This was probably a completely standard NullReferenceException, with the stack trace including some confusing additional detail. I'm drawing this conclusion from a different error I just got in a build of my game, where the following error comes out of an optimized build:

    However, when the same error is produced in the Editor, this is the stacktrace:

    So, I think in the case of my original error, the Rigidbody was null/destroyed, and the whole "_Injected" stuff is perhaps the result of some optimization that makes it seem like this was an error in the engine, and not just an error in my own code.

    So maybe this is "resolved" in that sense, though this approach to certain makes things a bit confusing...
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    8,904
    Anything that has "_Injected" is the scripting binding layer in Unity i.e. the marshalling layer between C# and C++ as indicated in the brackets "(wrapper managed-to-native)".

    When certain bindings specify a NULL check they can do it in the C# side explicitly or as is more common devs apply an attribute of "[NotNull]" which the bindings layer can add code to check. You'll then see the check come from there instead of the method you call. It means the same thing though. Also, the same binding layer automatically checks the instance object the method is called on too in the case of properties AFAIK. In your case, it's Collider.bounds which would be true.
     
    dgoyette likes this.
  4. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,003
    Thanks for the detailed explanation, and demystifying this for me.
     
    MelvMay likes this.