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 ?
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.
@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.
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;
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.