Ray cast effect on objects at runtime

Please could anyone help me with getting the object change color when the raycast hits it and change back to normal when it does not. I have multiple objects in my scene and want them to react to the ray from the camera rig of my HTC Vive like this. Basically, to know which object is in focus.

Maybe try putting this script on your camera. All you need to do is make two materials, one with the highlighted color and one with the not-highlighted color, and add them to the script in the inspector.

    public Material highlighted;
    public Material notHighlighted;
    Gameobject currentlyHighlighted;
    
    void Update () {
         RaycastHit hit;
         if (Physics.Raycast(transform.position, transform.forward, out hit) {
              if (hit.collider.gameObject != currentlyHighlighted) {
                   hit.collider.gameObject.GetComponent<MeshRenderer>().material = highlighted;
                   currentlyHighlighted.GetComponent<MeshRenderer>().material = notHighlighted;
                   currentlyHighlighted = hit.collider.gameObject;
              }
         }
    }