Object Reference bug with RaycastHit?!

At the line of

if (hit.transform.gameObject.layer == LayerMask.NameToLayer("Player")) {

I get the

Object reference not set to an instance of an object

? I have tried a lot of things and still don’t understand why my Raycast Hit does not work please help!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BulletScript1 : MonoBehaviour
{
    public float MaxDist, DMG;
    public Transform FX;
    public LayerMask PlayerMask;
    void OnCollisionEnter(Collision collision)
    {
        Collider[] Enemies = Physics.OverlapSphere(transform.position, MaxDist, PlayerMask);
        
        foreach (Collider t in Enemies) {
            RaycastHit hit;
            Physics.Raycast(transform.position, new Vector3 (Random.Range(0, 360), Random.Range(0, 360), Random.Range(0, 360)), out hit, MaxDist);
            if (hit.transform.gameObject.layer == LayerMask.NameToLayer("Player")) {
                var HPScript = t.GetComponent<Target>();
                if (HPScript) {
                    HPScript.TakeDamage(DMG);
                    Debug.Log("Hit somethin");
                }
            }
        }
        Instantiate(FX, transform.position, transform.rotation);
        Object.Destroy(transform.gameObject);
    }
}

The Physics.Raycast method returns a boolean value, this value is telling you wheter the raycast did hit something or not, if the value is false then hit.transform is null and calling it will raise a NullReferenceException.

Edit the code of the foreach loop as it follows:

foreach (Collider t in Enemies) {
    RaycastHit hit;
    if (Physics.Raycast(transform.position, new Vector3 (Random.Range(0, 360), Random.Range(0, 360), Random.Range(0, 360)), out hit, MaxDist)){
        if (hit.transform.gameObject.layer == LayerMask.NameToLayer("Player")) {
            var HPScript = t.GetComponent<Target>();
            if (HPScript) {
                HPScript.TakeDamage(DMG);
                Debug.Log("Hit somethin");
            }
        }
    }
}