Array camera system

hi all, ive problem to understang array.

I want to made simply cacge camera function.

i use this:
var videoCamere : GameObject[ ] ;

i used gameobject to activate and deactivate all camera componenst(audiolistener,etc…).

now in my scene i put 2 or 3 cameras and put it on my variable.

i’ve assigned a C key to swap camera function.

but wat is correct way to calculate next camera?

i comepare a variable with videoCamere.Length and add 1,but doesn’t works…

for (c=0; c<videoCamere.Length;c++)

WHAT IS CORRECT FUNCTIONS I HAVE TO PUT HERE?

Perhaps something like this? Probably a more elegant way of doing this, but I just whipped this off the top of my head in notepad:

var nextCamereIndex = -1;

for (var index : int = 0; index < videoCamere.Length; index++)
{
	var currentCamere : GameObject = videoCamere[index];
	
	if (currentCamere.enabled)
	{
		nextCamereIndex = index + 1;
		currentCamere.enabled = false;
		break;
	}
}

if (nextCamereIndex == -1)
{
	//no cameras were active!
	//do something else? fix?
}
else if (nextCamereIndex == videoCamere.Length)
{
	nextCamereIndex = 0; //cycle back to the first camera
}


videoCamere[nextCamereIndex].enabled = true;

EDIT: maybe a bit simpler:

for (var index : int = 0; index < videoCamere.Length; index++)
{
	var currentCamere : GameObject = videoCamere[index];
	
	if (currentCamere.enabled)
	{
		currentCamere.enabled = false;
		
		var nextCamereIndex : int = index + 1;
		if (nextCamereIndex == videoCamere.Length)
		{
			nextCamereIndex = 0; //cycle back to the first camera
		}
		
		videoCamere[nextCamereIndex].enabled = true;
		
		break;
	}
}

Though I’d rather see this broken up into smaller separate functions, but just to give you an idea.

thank’s for fast reply.
tonight i test it and send feddback.

I’m not sure a for-loop is really something you want for this - unless you include yields - since it’ll just cycle through all the cameras and leave the last one active.

var currentCamere : int;
var camereList : GameObject[];

function NextCamere() { 
  camereList[ currentCamere ].enabled = false;
  // add one to currentCamere and, if it is then equal to camererList.length, it is set to zero by the % (modulus) operator
  currentCamere = ( currentCamere + 1 ) % camereList.length;
  camereList[ currentCamere ].enabled = true;
}

function Update() {
  if ( Input.GetKeyDown( KeyCode.C ) ) NextCamera();
}

My intent was for that code to be run each time when you press the “c” button. It will cycle to the next camera in sequence, not all of them at once (notice the “break” statement). The only reason I have the for loop there is to show a simple example without any forced context of classes, methods, or properties (such as “currentCamere”). But I do agree that the proper way of doing it is handling your own current-camera-index as you provided.

Aha, I did miss that break. That makes more sense now. :slight_smile:

than’ks a lot,now it works,onli a correction:
when i use gameobject, have to use “active” and not “enabled”.
I’ve solved.thank’s to all for helping me.