I have two cameras in the hierarchy.
“Camera” is displaying when running the game using render texture raw image and canvas in the game view bottom right corner.
When i click on C key to switch the cameras between Main Camera and Camera i set the render textire active and the Camera render texture to null:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SwitchCameras : MonoBehaviour {
public Camera[] cameras;
public int currentCamera = 0;
public GameObject objectToHide;
public RenderTexture rt;
void Awake()
{
if (cameras == null || cameras.Length == 0)
{
Debug.LogError("No cameras assigned..", gameObject);
this.enabled = false;
}
}
void Update()
{
if (Input.GetKeyDown(KeyCode.C))
{
// disable current
cameras[currentCamera].enabled = false;
// increment index and wrap after finished array
currentCamera = (currentCamera + 1) % cameras.Length;
// enable next
cameras[currentCamera].enabled = true;
if (cameras[currentCamera].name == "Camera")
{
objectToHide.GetComponent<Renderer>().enabled = false;
RenderTexture.active = null;
cameras[currentCamera].targetTexture = null;
}
else
{
objectToHide.GetComponent<Renderer>().enabled = true;
RenderTexture.active = rt;
for (int i = 0; i < cameras.Length; i++)
{
if (cameras*.name == "Camera")*
cameras*.targetTexture = rt;*
}
}
}
}
void EnableOnlyFirstCamera()
{
for (int i = 0; i < cameras.Length; i++)
{
// only 1==0 returns true
cameras*.enabled = (i == 0);*
}
}
}
This is the two lines i set the render text to null:
RenderTexture.active = null;
cameras[currentCamera].targetTexture = null;
What it does is freezing the Camera picture to still in the bottom right corner.
Then when switching back to the Main Camera i’m trying to active the Camera again:
RenderTexture.active = rt;
for (int i = 0; i < cameras.Length; i++)
{
if (cameras*.name == “Camera”)*
cameras*.targetTexture = rt;*
}
But the Camera not active there is still a frozen image inside like the render texture is not active.