So I have a script that is intreating through different game objects on a timer. Its working properly but once it gets to the last game object it stops the scene. I would prefer if it would just start re-looping through them and continue doing this, here is what I have so far. Thanks for any help you guys can give.
var waitTime = 5;
var numberOfCameras = 4;
// Timed camera switch function
function cameraLoop(){
for (var i = 0; i< numberOfCameras;i++) {
var oldCamera : GameObject = null;
if (i == 0 ) oldCamera = GameObject.Find("cameraObj"+(i));
Debug.Log("old Camera: cameraObj" + (i));
var newCamera = GameObject.Find("cameraObj"+(i+1));
Debug.Log("new Camera: cameraObj" + (i+1));
yield WaitForSeconds(waitTime);
Debug.Log("It yielded");
if(oldCamera) oldCamera.active = false;
Debug.Log("camera" + i + " has been deactivated");
newCamera.SetActiveRecursively (true);
Debug.Log("camera" + (i + 1) + " has been activated");
}
}
function Update () {
cameraLoop ();
}
function Start(){
var curCamera:GameObject = null;
var i:int = 0;
while (true){ // loop forever
// deactivate current camera, if any
if (curCamera) curCamera.SetActiveRecursively(false);
// get the next camera
curCamera = GameObject.Find("camera"+i);
// activate it
curCamera.SetActiveRecursively(true);
// wait
yield WaitForSeconds(waitTime);
// prepare pointer to the next camera
i += 1;
// if numberOfCameras reached, reset pointer to the first camera
if (i >= numberOfCameras) i = 0;
}
}
The way I have the gameObejects set up is that each gameObject has a camera in it as a child, this camera is running its own script that rotates around a model in the scene. The scene currently only has 3 models in it and each model has a camera rotating around it that is in an empty game object. I have the script to switch between the different empty gameObjects attached to the first camera in the first gameObject.
EDITED2: So I managed to change some things and have it switching to the next camera after it deactivates the first but it wont move on to the third. The loop seems to be stoping right before the yield the second time around. Heres the new code. Sorry about all the post btw.
var waitTime: float = 3;
var numberOfCameras = 3;
function Start(){
var curCamera:GameObject = null;
var i:int = 0;
while (true){ // loop forever
Debug.Log("starting Loop" +i);
// deactivate current camera, if any
if (curCamera) curCamera.SetActiveRecursively(false);
// get the next camera
curCamera = GameObject.Find("cameraObj"+i);
Debug.Log("cameraObj"+i);
// activate it
curCamera.SetActiveRecursively(true);
Debug.Log("activated Camera"+ curCamera);
// wait
Debug.Log("before yield"+i);
yield WaitForSeconds(waitTime);
Debug.Log("yielded"+i);
// prepare pointer to the next camera
i += 1;
// if numberOfCameras reached, reset pointer to the first camera
if (i >= numberOfCameras) i = 0;
}
}
you should be able to just replace the for(…) loop with a while(true). Define the variable i just before while(), initialized to 0, then at the end of the loop, just before the final }, add 1 to i, then test if i is equal to numberOfCameras. If it is, reset it to 0, and the next pass through the loop will start over.
Not part of your question, but you probably want to call cameraLoop() in Start() rather than Update(), while you’re at it. The function, once called, will keep running, so there’s no reason to call it again every frame.
You’re calling the coroutine cameraLoop inside Update - this makes a new cameraLoop coroutine be started each frame!
You could make the cameras loop from one to another in a simpler way, and without creating multiple coroutines:
var waitTime: float = 5;
var numberOfCameras = 4;
function Start(){
var curCamera:GameObject = null;
var i:int = 0;
while (true){ // loop forever
// deactivate current camera, if any
if (curCamera) curCamera.SetActiveRecursively(false);
// get the next camera
curCamera = GameObject.Find("camera"+i);
// activate it
curCamera.SetActiveRecursively(true);
// wait
yield WaitForSeconds(waitTime);
// prepare pointer to the next camera
i += 1;
// if numberOfCameras reached, reset pointer to the first camera
if (i >= numberOfCameras) i = 0;
}
}
Your game might be freezing because of the ridiculous number of unnecessary coroutines you have going. You are starting a new coroutine every frame. So by the time the first loop gets to the 4th camera, 20 seconds have gone by. That’s 20secs * 30FPS = 600 coroutines running at the same time. Four of which will be changing the camera. Basically, you are over looping.
You might just want to use InvokeRepeating() for this. It calls a given function at a specific interval.
var waitTime : int = 5;
var numberOfCameras = 4;
private var curCamera : int = 0;
function Start () {
InvokeRepeating("CameraLoop" , waitTime , waitTime);
}
function CameraLoop () {
var oldCamera = GameObject.Find("cameraObj (" + curCamera.ToString() + ")" );
oldCam.active = false;
//Add one to the counter.
curCamera = ++curCamera % numberOfCameras;
//make sure we don't go over the number of cameras we have.
var newCam = GameObject.Find("cameraObj (" + curCamera.ToString() + ")" );
newCameraSetActiveRecursively (true);
}
So I managed to change some things and have it switching to the next camera after it deactivates the first but it wont move on to the third. The loop seems to be stoping right before the yield the second time around. Heres the new code. Sorry about all the post btw.
var waitTime: float = 3;
var numberOfCameras = 3;
function Start(){
var curCamera:GameObject = null;
var i:int = 0;
while (true){ // loop forever
Debug.Log("starting Loop" +i);
// deactivate current camera, if any
if (curCamera) curCamera.SetActiveRecursively(false);
// get the next camera
curCamera = GameObject.Find("cameraObj"+i);
Debug.Log("cameraObj"+i);
// activate it
curCamera.SetActiveRecursively(true);
Debug.Log("activated Camera"+ curCamera);
// wait
Debug.Log("before yield"+i);
yield WaitForSeconds(waitTime);
Debug.Log("yielded"+i);
// prepare pointer to the next camera
i += 1;
// if numberOfCameras reached, reset pointer to the first camera
if (i >= numberOfCameras) i = 0;
}