Hello, I have this code:
private RaycastHit2D ray;
private void FlyBuildingItems()
{
Vector3 MousePosition = Camera.ScreenToWorldPoint(Input.mousePosition);
ray = Physics2D.Raycast(MousePosition, -Vector2.zero);
if (Input.GetMouseButton(0))
{
if (ray.collider.CompareTag("Build"))
IsBeingDragger = true;
}
}
}
It works as I intended, but when I click on an object without a collider, it gives the error “NullReferenceException: Object reference not set to an instance of an object”, the error itself does not interfere with the code in any way, but it catches the eye.
How can I fix it ?