Error That Doesn't Affect My Game Is Spammed Every Frame.
I keep getting 999+ of the error "NullReferebceException: Object reference not set to an instance of an object" The line of code: var rb = object.GetComponent(Rigidbody);
In the script I have it where if you click it instantiates a prefab there, and attached to the prefab are some scripts, colliders, and rigidbodies. After getting its Rigidbody I apply force to it. My script works just as I wanted it to. How can I fix this error, or is there anyway I can just ignore it? I would just disable error popups in the console but I still need to check for other errors. Thanks in advance.
if you're satisfied with your script just outcomment the line and everything related to rb, because it's null anyway.
It says it is null but without it the object doesn't move anywhere, I would just eli$$anonymous$$ate the line of code and to apply the force I would do object.AddForce(0, 0, 1000); or something like that but in the new Unities you have to get the rigidbody before doing that like var rb = object.GetComponent(Rigidbody);
Answer by HenryStrattonFW · Feb 04, 2017 at 11:30 PM
Errors like this should never be "ignored" per say, thats a very dangerous approach to take, especially given that unity will stop running scripts that throw them at runtime.
I'd advise just putting null checks around the offending lines, for example.
var rb = null;
if( object != null)
{
rb = object.GetComponent(Rigidbody);
}
but keep in mind going down this route would mean that now rb is null, and similar checks would need to be placed around code that uses that.