Question by
SeanSurtz · May 16, 2017 at 01:18 AM ·
raycastnullreferenceexceptionif-statementsif statement
Why Am I Getting Null Reference Exception?
I don't get it. I want it to check if it finds a collider with the tag Lootable. What am I missing here? I'm so confused :\
private void FixedUpdate()
{
mouseRay = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit rayHit;
if (Input.GetButtonDown("Fire1"))
{
Physics.Raycast(mouseRay, out rayHit);
if (rayHit.collider.tag.Equals("Lootable"))
{
print("hit");
print(inventoryWindowShow);
inventoryWindowShow = true;
}
}
if (Input.GetKeyDown(KeyCode.Escape))
{
inventoryWindowShow = false;
}
}
Comment
Answer by alankemp · May 16, 2017 at 09:38 AM
Physics.Raycast returns a bool to tell you if it actually hit anything.
As you haven't provided the actual error, I'll guess that the ray isn't hitting anything and rayHit.collider is null?
Your answer
![](https://koobas.hobune.stream/wayback/20220612122102im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to account for a failed raycast? 1 Answer
Stubborn Pick Up Script 0 Answers
If an integer ends with an 1. 1 Answer
How to use or statements in an if statement. 1 Answer
how to make a succession(n+1,5n,etc) that work in if statement? 0 Answers