Raycasting is hitting objects and not hitting them at the same time

My level is a small closed room with colliders where a raycast should not be able to miss. I was getting null reference exceptions so I put the raycast in an if statement, which took away the error but both this line Debug.LogError("Raycast hit nothing"); and this line Debug.Log(hit.transform.gameObject.tag); were being called at the same time constantly.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;


  public class playerShooting : MonoBehaviour
  {
      public Transform cam;
      //public GameObject greyCross;
      public GameObject redCross;

      int layerMask = 1 << 8;
      string scene = "";
      float range;
      bool isAimingAtEnemy = false;

      void Start()
      {
        scene = SceneManager.GetActiveScene().name;
        range = Mathf.Infinity;
      }

      void Update()
      {
        layerMask = ~layerMask;

        /*if (scene == "SampleScene")
        {
          if (Input.GetButtonDown("Fire1"))
          {
            RaycastHit hit;
            Physics.Raycast(cam.position, cam.transform.forward, out hit, 100f);
            if (hit.transform == null) return;
            if (hit.transform.gameObject.tag == "tower")
            {
              hit.transform.Translate(-Vector3.up*0.5f);
            }
          }
          if (Input.GetButtonDown("Fire2"))
          {
            RaycastHit hit;
            Physics.Raycast(cam.position, cam.transform.forward, out hit, 100f);
            if (hit.transform == null) return;
            if (hit.transform.gameObject.tag == "tower")
            {
              hit.transform.Translate(Vector3.up*0.5f);
            }
          }
        }*/
        else if (scene == "mini map" || scene == "shooting game")
        {
          CheckIfAimingAtObject();
          if (isAimingAtEnemy) redCross.SetActive(true);
          else redCross.SetActive(false);

          if (Input.GetButtonDown("Fire1"))
          {
            Shoot();
          }
        }
      }

      private void Shoot()
      {

      }
      private void CheckIfAimingAtObject()
      {
        RaycastHit hit;
        if (Physics.Raycast(cam.position, cam.TransformDirection(Vector3.forward), out hit, range, layerMask))
        {
          Debug.Log(hit.transform.gameObject.tag);
          if (hit.transform.gameObject.tag == "enemy") isAimingAtEnemy = true;
          else isAimingAtEnemy = false;
        }
        else
        {
          Debug.LogError("Raycast hit nothing");
        }
      }
  }

I’m using unity 2019.3.9. It doesn’t seem to be causing any problems but it would be nice to know why this is. Thanks!

Do not over complicate the raycast method to excited it you just need this

Void update ()
    {
        If (// your input method )
        {
            RaycastHit hit; 
            If( physics.raycast ( //start position, // direction for example Cam.Forward, out hit, // range or anything that you want optional)
            {
                // anything you want to do. 
            }
        }
    }