how to set up interaction method using raycast?

hi, im currently learning game development for a week, so i know nothing. i’m trying to develop interaction system in 3d thidrperson mode. here is my raycast script that i set for aiming and interaction censor attached to main camera.

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

public class raycastaimpoint : MonoBehaviour
{
    public Transform maincamera;
    public Transform player;
    private int rayLength = 500;
    public LayerMask interactable;
    public Image crosshair;
    public bool detected;

    void start()
    {
        detected = false;
    }
    void crosshairActive()
    {
        crosshair.color = Color.red;
        detected = true;
    }

    void crosshairNormal()
    {
        crosshair.color = Color.white;
        detected = false;
    }
    // Update is called once per frame
    void Update()
    {
        RaycastHit hit;
        Vector3 fwd = transform.TransformDirection(Vector3.forward);
        if(Physics.Raycast(transform.position, fwd, out hit))
        {
            Vector3 hitPoint = hit.point;
            player.LookAt(hitPoint);
            
        }
        else
        {
            Vector3 farthestPoint = transform.position + fwd * rayLength;
            player.LookAt(farthestPoint);
        }
        Debug.DrawLine(transform.position, hit.point, Color.yellow, 0.5f * Time.deltaTime);
        transform.rotation = maincamera.rotation;

        if(Physics.Raycast(transform.position, fwd, 20f, interactable))
        {
            crosshairActive();

        }
        else
        {
            crosshairNormal();
        }
        

    }
}

crosshair works well when aim to interactable object turn red, and can turn white again.

problem is i want to create other script for managing interaction based on crosshair color change. if it turn red i want to hit “e” key and some thing happen. ive tried some answer here but no success… tying something like this script attached to interactable game object.

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

public class interactaableScript : MonoBehaviour
{
    public raycastaimpoint censor;

    void update()
    {
        
        if(censor.detected)
        {
            Debug.Log("something");
        }
        
    }
}

thanks for your time guys, peace!

A couple things, first off, it is best practice to use the Pascal naming convention when naming Methods and Classes, like this

public class InteractableScript : Monobehaviour

not like this

public class interactablescript : Monobehaviour

And variables with Camel notation like this

public int thisIsATestInt;

(Which you actually already did)

now, your program will still function if you don’t follow this convention, with the exception of your update method in your second script, which is why I bring up Pascal notation. You spell

void Update()

like

void update()

so that code will not do anything, so first change that (also you spelled interactable like interaactable). Next, you are on the right track with the rest of your code.
You are sensing if something is detected by your raycast, so we can extend that to also sense if you are pressing down the ‘e’ key like so (I also fixed your naming conventions so it will look a little different)

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class InteractableScript : MonoBehaviour
 {
     public raycastAimPoint censor;
 
     void Update()
     {
         
         if (censor.detected && Input.GetKeyDown(KeyCode.E) //will sense if something is detected and if we are pressing the E key
         {
             Debug.Log("do something");
         }
         
     }
 }

If you have any other questions then don’t hesitate to ask, hope this helps!