Change target in a camera script

Hi, so I want to change the target of my follow cam script so when I push a button it changes the cube that is being followed. I get to a point but then I dont know how to change the target from the array where I put all the game objects.
Thanks for any help with this.

So here is what I have so far the last two functions are what Im really working on

using UnityEngine;
using System.Collections;

public class FollowCam : MonoBehaviour {
	
	private GameObject [] avaliableTargets;
	
    public Transform target;
	private Transform currentTarget;

    public float targetHeight = 1.7f;
    public float distance = 5.0f;

 
    public float maxDistance = 20;
    public float minDistance = .6f;

 
    public float xSpeed = 250.0f;
    public float ySpeed = 120.0f;

 
    public int yMinLimit = -80;
    public int yMaxLimit = 80;

 
    public int zoomRate = 40;


    public float rotationDampening = 3.0f;
    public float zoomDampening = 5.0f;

 
    private float x = 0.0f;
    private float y = 0.0f;
    private float currentDistance;
    private float desiredDistance;
    private float correctedDistance;

    void Start ()
    {
        Vector3 angles = transform.eulerAngles;
        x = angles.x;
        y = angles.y;

        currentDistance = distance;
        desiredDistance = distance;
        correctedDistance = distance;

        // Make the rigid body not change rotation

        if (rigidbody)
            rigidbody.freezeRotation = true;
    }

	
	void Update()
	{
		//find the cube Optimus Prime says so!
		if(Input.GetKey(KeyCode.A))
		{
			Debug.Log("A");
			findCube();
		}
	}
    void LateUpdate ()
    {
        // Don't do anything if target is not defined
        if (!target)
            return;
        // If either mouse buttons are down, let the mouse govern camera position

            x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;

            y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
        

        // otherwise, ease behind the target if any of the directional keys are pressed
        if (Input.GetAxis("Vertical") != 0 || Input.GetAxis("Horizontal") != 0)
        {
            float targetRotationAngle = target.eulerAngles.y;
            float currentRotationAngle = transform.eulerAngles.y;
            x = Mathf.LerpAngle(currentRotationAngle, targetRotationAngle, rotationDampening * Time.deltaTime);
        }

        y = ClampAngle(y, yMinLimit, yMaxLimit);

        // set camera rotation
        Quaternion rotation = Quaternion.Euler(y, x, 0);
        // calculate the desired distance
        desiredDistance -= Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime * zoomRate * Mathf.Abs(desiredDistance);
        desiredDistance = Mathf.Clamp(desiredDistance, minDistance, maxDistance);
        correctedDistance = desiredDistance;

        // calculate desired camera position
        Vector3 position = target.position - (rotation * Vector3.forward * desiredDistance + new Vector3(0, -targetHeight, 0));

        // check for collision using the true target's desired registration point as set by user using height
        RaycastHit collisionHit;
        Vector3 trueTargetPosition = new Vector3(target.position.x, target.position.y + targetHeight, target.position.z);

        // if there was a collision, correct the camera position and calculate the corrected distance
        bool isCorrected = false;
        if (Physics.Linecast(trueTargetPosition, position, out collisionHit))
        {
            position = collisionHit.point;
            correctedDistance = Vector3.Distance(trueTargetPosition, position);
            isCorrected = true;
        }

        // For smoothing, lerp distance only if either distance wasn't corrected, or correctedDistance is more than currentDistance
        currentDistance = !isCorrected || correctedDistance > currentDistance ? Mathf.Lerp(currentDistance, correctedDistance, Time.deltaTime * zoomDampening) : correctedDistance;

        // recalculate position based on the new currentDistance
        position = target.position - (rotation * Vector3.forward * currentDistance + new Vector3(0, -targetHeight, 0));

        transform.rotation = rotation;
        transform.position = position;
    }

 

    private static float ClampAngle(float angle, float min, float max)
    {
        if (angle < -360)
            angle += 360;
        if (angle > 360)
            angle -= 360;
        return Mathf.Clamp(angle, min, max);

    }
	
	//get all the game object with the same tag
	private void findCube()
	{
		avaliableTargets = GameObject.FindGameObjectsWithTag("Cube");
		foreach(GameObject g in avaliableTargets)
		{
			Debug.Log("NAme " + g.name);
			
		}
	}
	//chagne the target for the camera
	void nextTarget()
	{}

}

If you will just be incrementing through your availableTargets array, you could just simply keep a currentTargetIndex variable and use that.

private int currentTargetIndex= 0;

void nextTarget()
{
   // Increment index
   currentTargetIndex++;

   // If you want to cycle around when you get to the end
   if(currentTargetIndex >= availableTargets.Length)
   {
      currentTargetIndex = 0;
   }

   // Safe to always check the index is within bounds of array
   if(currentTargetIndex < availableTargets.Length)
   {
      currentTarget = availableTargets[currentTargetIndex];
   }
}

it looks as though you need to create a list of game objects. In the Awake function you would populate it with the Cubes. and then you can cycle through it using an index value.

something like:

//member variable
GameObject[] ObjectList = null;
int index =0;


void Awake(){
     ObjectList = GameObject.FindGameObjectsWithTag("Cube");
}

and then

void findNext(){

    index++;
    if(index >= ObjectList.length)
          index = 0;

    currentTarget = ObjectList[index].transform;

}