how to check raycast from another object c#

i have 2 script
first is raycast form (“FirstPersonCharacter”) Tag with MainCamera

public class RayCast : MonoBehaviour {
    public float distance;
    public GameObject Eopen;
    public bool targethit;
    RaycastHit hit;
    

    void Update ()
    {
        Vector3 forward = transform.TransformDirection(Vector3.forward) * distance;
        Debug.DrawRay(transform.position, forward, Color.red);

 

        if (Physics.Raycast(transform.position, transform.forward, out hit, distance) && hit.collider.gameObject.tag == ("ray trigger"))
        {
            
                targethit = true;

                Eopen.SetActive(true);
                if (Input.GetKeyDown("e"))
                {
                    Eopen.SetActive(true);
                    hit.collider.SendMessage("keye");
                }
         }
               else
                {
                  targethit = false;
                  Eopen.SetActive(false);
                }
            
        }
        
    }

and second

using UnityEngine;

public class Test : MonoBehaviour
{
    public GameObject capsule;
        

    void update()
    {
      
        if (GameObject.Find("FirstPersonCharacter").GetComponent<RayCast>().targethit)
        {
            capsule.SetActive(true);
        }
        if (!GameObject.Find("FirstPersonCharacter").GetComponent<RayCast>().targethit)
        {
            capsule.SetActive(false);
        }       

    }

}

But it not work.

(“capsule”)is not acttive.

What i’m wrong?

And is there any way that I can use to detect bool or detect raycast from another object?

Capsule is a gameObject.
Could it be, that this gameObject contains one of your scripts?

You could get data from another script like this:

  • On the script providing the data, create public methods/properties/variables which provide the necessary data.
  • On the script needing the data, reference the first script instance
  • Use the instance to access the required data.

It is not possible to passively get a notification when a raycast hits your object.
But you could call a method every time you shoot a raycast and hit an object containing a certain script.

Greetings
Chillersanim