Hey! So yeah, I know there’s probably a post like this multiple times a week, but I have no idea what’s going on with this one. Everything works exactly like it should, but there are still errors.
Technically I can ignore them, because there aren’t any apparent bugs, but I think we all know that’s a bad idea.
I’m using VRTK for my VR interactions/pointers/etc. (pointerRenderer is a component specified earlier in the script.) In the code below, I tried to leave out things that I know have nothing to do with the error, work fine, and would just clutter this post.
Any help/ideas would be greatly appreciated! Stay safe!
The error I’m getting is this:
NullReferenceException: Object reference not set to an instance of an object
AbilityManager.pointingAtValidObject () (at Assets/Scripts/PlayerScripts/AbilityManager.cs:80)
AbilityManager.Update () (at Assets/Scripts/PlayerScripts/AbilityManager.cs:96)
And the code causing the error is this:
public bool pointingAtValidObject()
{
if (pointer.enabled && pointer.IsStateValid())
{
/*Specifically this line*/ if (pointerRenderer.GetDestinationHit() /* returns a raycast*/.collider.gameObject.GetComponent<ForceGrabObject>() && !pointerRenderer.GetDestinationHit().collider.gameObject.GetComponent<VRTK_InteractableObject>().IsGrabbed())
{
//CODE STUFF
return true;
}
else
return false;
}
else
return false;
}
private void Update()
{
if (pointingAtValidObject())
{
if (controllerEvents.triggerPressed) // IS USING ABILITY
{
triggerDown();
}
}
else
return;
}
So that’s a very complex line, and its complexity is going to make it difficult to debug. It’s very likely that your raycast is not hitting anything, and thus some piece of that long long chain of things will be null. I recommend putting some of the work it’s doing into earlier lines.
(Also, “returns a raycast” doesn’t make sense; there’s no raycast object/class to return. Did you mean that it returns a RaycastHit? I’ll write this on that assumption)
(Also, if GetDestinationHit() actually does a Raycast, then you’re doing an extra completely unnecessary raycast in this code. Raycasts are notoriously performance-draining, relative to a lot of other operations you can do in code, so you should avoid doing the same raycast repeatedly.)
RaycastHit rchit = pointerRenderer.GetDestinationHit();
GameObject hitObject = rchit.collider.gameObject;
bool hasForceGrab = hitObject.GetComponent<ForceGrabObject>() != null;
var intObj = rchit.collider.gameObject.GetComponent<VRTK_InteractableObject>();
if (hasForceGrab && !intObj.IsGrabbed())
I’ve made a point to not make changes that actually solve the problem - this just is separating the existing logic you have into different lines so that the NullReferenceException will point to a smaller number of things happening on one line, making it easier to debug.
As I said, the smart money is that the raycast simply isn’t hitting anything, and therefore rchit.collider would be null, in which case the NRE will now be triggered on line 2 of the above code. It could be that it’s hitting an object that doesn’t have an attached VRTK_InteractableObject, in which case line 4 would trigger a NRE. And so on.
Long story short: don’t try to squeeze a bunch of stuff onto one line.
Wow, I was not expecting such a detailed reply, thank you so much!
Yes, I meant RaycastHit, not raycast.
I will try this when I can and update this post accordingly.
Once again, thanks so much! 
Okay, so I did that, and it narrowed it down to just giving me the error on this line:
GameObject hitObject = rchit.collider.gameObject;
I thought it was because I didn’t check hitObject != null before moving to the next line, so I did that:
RaycastHit rchit = pointerRenderer.GetDestinationHit();
bool hasHitObject = rchit.collider.gameObject;
if (hasHitObject)
{
GameObject hitObject = rchit.collider.gameObject;
bool hasForceGrab = hitObject.GetComponent<ForceGrabObject>() != null;
var intObject = rchit.collider.gameObject.GetComponent<VRTK_InteractableObject>();
if (hasForceGrab && !intObject.IsGrabbed())
{
. . .
}
but still got the error on this line:
bool hasHitObject = rchit.collider.gameObject;
Any ideas? Thanks so much, and stay safe!
Does the GetDestinationHit method handles the case where nothing is hit ? Because when looking at the doc on RaycastHit, if nothing is hit, then collider is null.
Maybe you can check this condition instead ?
if(rchit.collider != null)
If you think about it: If what you would think of as “rchit.collider.gameObject” is null, then how could “rchit.collider” be anything but null?
I’m sorry, I don’t quite know what you mean.
I changed it to if(rchit.collider != null)
and that fixed it and got rid of the error, as per @TheCakeIsReal 's suggestion.
Thank’s so much!
Let me rephrase: A Collider is a component that must always be attached to a GameObject - it can’t exist on its own. So if the Collider exists, then collider.gameObject must exist.
If the GameObject doesn’t exist, then the Collider doesn’t exist. So collider.gameObject will give you a NullReferenceException anytime it could possibly return null. So hit.collider is the thing you must null check.
Ah, got it. I think that’s what confused me to begin with. Thanks!