array help needed

Hi I am trying to make a array of all the cameras in the scene that are within a certain distance from the caller.

I just started programing and am not too confidant with arrays yet.

var cameraRange = 30;
var viewList[] = new Array ();
var numbInRange : int;

function Update () {
	var cameras[] = Camera.allCameras;
	for (var camera : Camera in cameras[])
    {
		if (Vector3.Distance(transform.position, camera.position) >= cameraRange) {
		numbInRange++;
		}
		else {
			
		}
	}
}

That’s a good start. A few things:

  • The >= operator should be <=, unless you’re trying to find cameras that are outside the given range
  • Instead of keeping track of how many cameras there are with the numbInRange variable, just add the camera itself to the array (see the next point)
  • Instead of using a built-in array, use an Array object; this will allow you to keep adding items without first setting a specific size
  • To get the camera’s position, use camera.transform.position; a camera position doesn’t have a position variable, but its transform does