- Home /
Enable / Disable cameras on the scene
Hello guys, following this topic https://answers.unity.com/questions/1443599/unable-to-change-camera.html?childToView=1444185#comment-1444185 , my issue is now concrete.
I have a script with 2 public cameras, and I have assigned them on the inspector, more precisly I placed the prefabs of these camera in the inspector
So when I said " cam1.enabled = false; " on my script, the prefab of this camera will turn off, but the cam1 on the scene will not be affected.
Is there any way to disable / enable cameras on the scene, please ?
Answer by WorldEater · Dec 19, 2017 at 03:37 PM
The prefab and the instance of the prefab are two separate objects, you need a reference to the camera in the scene to modify it. An easy way to get this reference is to store it when you spawn the camera.
GameObject cameraObject = Instantiate (cameraPrefab, spawnPos, spawnRot) as GameObject;
Then use the camera object to edit whatever you want. Alternatively drag the camera in the scene into your script.
Ok thank you. Ill try like this because I cant put the camera in the scene into their variable, I just can put their prefab but it's useless.
No problem, if you bump into any issues feel free to ask.
Answer by programer717 · Dec 19, 2017 at 04:35 PM
@imM4TT I would not use prefabs but what I do is set the cameras where I want them set them all on the same display and use the number keys to change here is the script that I use most often. :public Camera camera1; public Camera camera2;
// Use this for initialization
void Start () {
camera1.enabled = true;
camera2.enabled = false;
}
// Update is called once per frame
void Update () {
if (Input.GetKey ("1")){
camera2.enabled = false;camera1.enabled = true;
}
if (Input.GetKey ("2")) {
camera1.enabled = false;camera2.enabled = true;
}
}
}
sorry if this is not what you meant in your question but one thing you have to make sure for this script is all the cameras are on the same display.
Yep It sounds nice but I cant assign the cameras which are in the scene on their public variable ...
Answer by imM4TT · Dec 19, 2017 at 04:57 PM
When I try your method I got an error like that :
Cannot convert type UnityEngine.Camera' to
UnityEngine.GameObject' via a built-in conversion
GameObject cameraObject = Instantiate(Camera, transform.position, transform.rotation) as GameObject;
GameObject cameraObject = Instantiate(Camera.gameObject, transform.position, transform.rotation) as GameObject;
Answer by Adil_Alhilali · Jun 18, 2020 at 05:18 PM
void Off_All_Cameras()
{
object[] Cams = GameObject.FindObjectsOfType(typeof(Camera));
foreach (Camera C in Cams)
{
if(C.name != PlayerCamera.name)
{
C.enabled = false;
}
}
}
Answer by jmsachin · Sep 29, 2021 at 11:18 AM
I have the same problem but neither of these comments help me and I don't know what to do now , btw I am using Unity Networking AKA uNet so I have to use prefabs to spawn in the player and I cannot insert my camera into my array until it is a separate prefab and as I said earlier those lines of script given by @WorldEater and @Hellium don't help me in any way, tell me if you guys have any fix for this.
Your answer
