Help finding a gameobject with a few parents and checking if the target is that game object in c#

using System.Collections;
using System.Security.Cryptography;
using Unity.Mathematics;
using UnityEditor;
using UnityEngine;

public class gunScript : MonoBehaviour
{
    public float damage = 10f;
    public float headShotDamage = 20f;
    public float range = 300f;
    public Camera cam;
    public ParticleSystem muzzleFlash;
    public GameObject impactEffect;
    public Animator recoilAni;
    private bool recoilDone = true;
    public GameObject hatObject;
   

    void Start()
    {
        hatObject = GameObject.Find("Cowboyenemy/CowboyRio_Unity/CowboyRIO_Hat");
    }
    void Update () {

        if (Input.GetButtonDown("Fire1")) {

            Shoot();
            StartCoroutine(shotTimer());
        }
    }

    void Shoot () 
    {   
        if (recoilDone == true)  
        {
            
            RaycastHit hit;
            if (Physics.Raycast(cam.transform.position, cam.transform.forward, out hit, range))
                {
                    recoilAni = GetComponent<Animator>();
                    Debug.Log(hit.transform.name);
                    enemyScript target = hit.transform.GetComponent<enemyScript>();
                    if (target != null)
                    {
                        
                        if (target != hatObject)
                        {
                            Debug.Log("Hit body!");
                            target.takeDamage(damage);
                        }
                        if (target == hatObject)
                        {
                            Debug.Log("Hit head!");
                            target.takeDamage(headShotDamage);
                        }
                        
                        
                    }

                GameObject impactWork = Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));
                Destroy(impactWork, 2f);        
            
                }
        }        
            
        
    }
    IEnumerator playRecoil()
    { 
        recoilDone = false;
        muzzleFlash.Play();
        recoilAni.Play("GunRecoil");
        yield return new WaitForSeconds(0.3f);
        recoilAni.Play("gunPause");
        recoilDone = true;
    }
    IEnumerator shotTimer()
    {
        if (recoilDone == true)
        { 
            
            StartCoroutine(playRecoil());
            yield return new WaitForSeconds(0.3f);
        }
    }
}

trying to find the gameobject with
hatObject = GameObject.Find(“Cowboyenemy/CowboyRio_Unity/CowboyRIO_Hat”);

and trying to check if the gameobject is the hat with
if (target == hatObject)
{
Debug.Log(“Hit head!”);
target.takeDamage(headShotDamage);
}

And? The computer makes an odd squealing sound? Smoke comes out of the monitor?

You need to provide a clue but if it was me, I wouldn’t use Find but if I did I would check whether it actually found the hat and move on from there.

Did it ever work?

Remember the first rule of GameObject.Find():

Do not use GameObject.Find();

More information: https://starmanta.gitbooks.io/unitytipsredux/content/first-question.html

In general, DO NOT use Find-like or GetComponent/AddComponent-like methods unless there truly is no other way, eg, dynamic runtime discovery of arbitrary objects. These mechanisms are for extremely-advanced use ONLY.

If something is built into your scene or prefab, make a script and drag the reference(s) in. That will let you experience the highest rate of The Unity Way™ success of accessing things in your game.

“Stop playing ‘Where’s GameWaldo’ and drag it in already!”

Here’s why all this stuff is CRAZY code: