Raycast Hit : object reference not set to an instance of object unity


I have been working on a 2D Mobile touch game. the game collects points whenever I touch the spawned object, so I have used RaycastHit2D to send a ray wherever I touch, and if the hit is on a specific tag it’ll destroy the item (it works fine when I touch the object). But when I touch somewhere in the screen other than the collectible object, some NULL REFERENCE EXCEPTION shows “object reference not set to an instance of object unity”. The game runs fine even with the error, but I prefer to understand and clear the error first. pls, check and reply if any of you understand why this error came up.

if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began )

            RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.touches[0].position), Vector2.zero);
            if (hit.collider.transform.tag == "Collectible" )
GameObject hitObject = hit.transform.gameObject;

If the Raycast doesn’t hit anything, then your “hit” variable will be null. The if statement will try to access hit.collider.transform.tag and it will fail because “hit” is null.

Is the Raycast always guaranteed to hit something in this scenario? If not, then it would be good to check for null before trying to access the tag.