- Home /
Cannot switch cameras!!! Help ASAP!!!
Hi! I need to be able to switch between 4 cameras in my game. Here is my code:
var cam1 : Camera;
var cam2 : Camera;
var cam3 : Camera;
var cam4 : Camera;
function Start () {
cam1.enabled = true;
cam2.enabled = false;
cam3.enabled = false;
cam4.enabled = false;
}
function Update () {
if(Input.GetKeyDown(KeyCode.A)){
cam1.enabled = true;
cam2.enabled = false;
cam3.enabled = false;
cam4.enabled = false;
}
if(Input.GetKeyDown(KeyCode.S)){
cam1.enabled = false;
cam2.enabled = true;
cam3.enabled = false;
cam4.enabled = false;
}
if(Input.GetKeyDown(KeyCode.D)){
cam1.enabled = false;
cam2.enabled = false;
cam3.enabled = true;
cam4.enabled = false;
}
if(Input.GetKeyDown(KeyCode.F)){
cam1.enabled = false;
cam2.enabled = false;
cam3.enabled = false;
cam4.enabled = true;
}
}
This does change the cameras, but after i stop playing the game! How can I have it so the change happens during the game? I have assigned the four cameras to the variables in the Unity GUI, but still nothing. The cameras are part of an object and are in the assets. I need help, my project is due Friday.
After you stop playing the game? So you Play and press F, and nothing happens, but when you Unplay cam4 is suddenly the enabled one?
@jacobschellenberg The script seems to be working fine, and he even stated that it works when he's playing the game.
@jaffy25 If you are having questions about the editor's behaviour, and why a camera being enabled doesn't carry over when you stop the game, The editor automatically saves state of things when you hit play, when you stop the game, the editor will try to revert the state of everything to what it was when you hit play (position/rotation/scale, what objects exist where, what objects are attached to other objects as children, what behaviours are attached to those objects.)
When you hit stop, the Unity Editor is reseting the state of the cameras to what they were when play was hit.
I think hes saying in game the cameras are not changing while the game is playing.
Answer by Bunny83 · May 12, 2014 at 02:26 AM
You have linked the camera instances of your prefab in those variables and you're probably instantiating that prefab at runtime. Now you enable / disable the cameras in your prefab but that won't change the actual game instance.
There are some ways:
Either have the object with the cameras already in the scene and link the for cameras to those instances
or if you need to instantiate the object at runtime, you have to setup the camera references manually through scripting.
Attach that script to the prefab itself so when the object is instantiated the prefab links are automatically "translated" to the new instances of the clone.
You can't reference the cameras on the prefan and expect that the cameras of an instance of that prefab would change at runtime since an instance is just a clone / copy of the prefab,
Your answer
Follow this Question
Related Questions
switch between multiple cameras based on distance to target 2 Answers
Multiple Camera Switching 1 Answer
Trouble switching cameras 1 Answer
cameras switch doesn't work 1 Answer
switch between cameras in javascript? 2 Answers