- Home /
camera switch not working
hey guys this script i got does not work i want to switch view from camera 1 to camera 2 afrter 10 sec but i cant get it to work can you hepl me please
var waitTime = 10; var numberOfCameras = 7;
function Start() {
for (var i = 0; i<=numberOfCameras;i++) { var oldCamera = GameObject.Find("Camera"+(i-1)); var newCamera = GameObject.Find("Camera"+i);
oldCamera.active = false;
newCamera.active = true;
yield WaitForSeconds(waitTime);
}
}
Answer by DaveA · Apr 06, 2011 at 09:28 PM
Do you have a camera called Camera-1 ? Or do they start with Camera0? Because this script wants to start with Camera-1 and go to Camera7
Double-check your console, make sure there aren't any errors being displayed when your game is running. And for safety's sake, you should add checks for null to avoid referencing oldCamera or newCamera if one is not found. (In general, whenever you call Find you should check that you got something back before dereferencing it.)
Answer by Statement · Apr 06, 2011 at 10:26 PM
It's because you're disabling the game object, so you can't find it the next search. You also seemed to have a very odd for loop. I changed so it looks for
- Camera0
- Camera1
- Camera2
- Camera3
- Camera4
- Camera5
- Camera6
Try storing them in a list instead:
import System.Collections.Generic;
var waitTime = 10; var numberOfCameras = 7;
function Start() {
// 1. Find all cameras.
// You probably want to disable them all as well,
// so no other camera is active.
var cameras = new List.<GameObject>(numberOfCameras);
for (var i = 0; i < numberOfCameras; ++i) {
cameras.Add(GameObject.Find("Camera"+i));
cameras[i].active = false;
}
// 2. Loop through them and toggle active.
var oldCamera : GameObject;
for (var newCamera in cameras) {
if (oldCamera) oldCamera.active = false;
if (newCamera) newCamera.active = true;
yield WaitForSeconds(waitTime);
oldCamera = newCamera;
}
}
Could he just enable/disable the Camera component, leaving the object active?
Assu$$anonymous$$g he started with all active cameras, it shouldn't have mattered about deactivating them, he's 'done' with the deactivated one(s). But a list is good. $$anonymous$$y own solution was to have an array of transforms whereby I drag (inactive) cameras, but I don't want to give away too much ;)
Answer by adam_unity566 · Jun 21, 2020 at 07:46 PM
Well, the simple answer to your problem is that your SetActive statement should be oldCamera.SetActive(false)