Raycasting using secondary camera

I have 2 camera in the scene. First(i.e the main one) cover the upper section of the scene. While the secondary one covers the rest of the scene.What I want to do is get the touch of the Game Objects that are visible through the secondary camera.I am attaching the script of drag to the prefab and came across certain links like this which states that one cannot attach game objects to a a prefab in the inspector and tried to do that and found that correct. So tried doing it in script using GameObject.find() but then while doing raycasting it needs a Camera object and not the GameObject. Tried looking at this Question.It says about the active camera and if am not wrong both camera are active as can see view from both camera .So can anyone please help how to go about it.

You can do it exactly as you do with any object, but just specify the camera you want as a variable, like this:

public Camera altCam;


Ray ray = new Ray(altCam.transform.position, altCam.transform.forward);

I was having a similar issue. I had multiple cameras in my 2D scene, 1 main while the others were overlay for different HUD items. I wanted to be able to click/touch the HUD items and know which one was selected. My code:

// Must be clicked and released
if (Input.GetMouseButtonUp(0))
{
  touched = true;
  worldPoint = myCamera.ScreenToWorldPoint(Input.mousePosition);            
}
// Must be touched and released
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended)
{
  touched = true;
  worldPoint = myCamera.ScreenToWorldPoint(Input.GetTouch(0).position);
}

if (touched)
{
  // cast a ray and get all colliders
  RaycastHit2D[] hits = Physics2D.RaycastAll(worldPoint, myCamera.transform.forward);
  
  foreach (RaycastHit2D hit in hits)
  {
    // The gameobject hit
    GameObject hitGameObject = hit.collider.gameObject;
    
    //... do logic
  }
}