Using ScreenPointToRay with multiple changing cameras.

I’m currently making a small Tower Defense with the MainCamera in topdown isometric view and I’m trying to add a SideCamera at a 45 degree angle to move around with and still be able to place towers.

I have a grid in build mode, and the currently hovered grid cell is highlighted. It works great when using the MainCamera, however, the hightlighted grid while in SideCamera is as if the ray was still cast from the MainCamera, not from the SideCamera.

Example

The ChangeCamera script is as follow:

private Camera currentCam;
private Camera alternateCam;

void Start () 
{
    currentCam = GameObject.FindWithTag("MainCamera").camera;
    alternateCam = GameObject.FindWithTag("alternateCam").camera;
}

void Change(GameObject btnObj) 
{
    currentCam.enabled = !currentCam.enabled;
    alternateCam.enabled = !alternateCam.enabled;
}

As for the ScreenPointToRay, I used

Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); 

at first but replaced it to

Ray ray = camera.ScreenPointToRay(Input.mousePosition);

The explained behavior occurs with the second version. The first one returns errors when using the SideCamera view.

In which direction should I go to fix this? Is my ChangeCamera script not doing this the right way? Should I use something else than ScreenPointToRay?

Store a reference to the current enabled camera, use that in your ray definition :

private Camera currentCam;
private Camera alternateCam;
private Camera rayCam;

void Start ()
{
	currentCam = GameObject.FindWithTag("MainCamera").camera;
	alternateCam = GameObject.FindWithTag("alternateCam").camera;
    rayCam = currentCam;
}

void Change(GameObject btnObj)
{
	currentCam.enabled = !currentCam.enabled;
	alternateCam.enabled = !alternateCam.enabled;
	
	if ( alternateCam.enabled )
	{
		rayCam = alternateCam;
	}
	else
	{
		rayCam = currentCam;
	}
}

// ....

Ray ray = rayCam.ScreenPointToRay(Input.mousePosition);