Array Syntax Issue

I have this piece of code that works EXCEPT for the part that activates the camera from the array myCams.

// activate other camera
myCams[thisSeat].GetComponent("Camera").active = true;

All my attempts at getting this line of code to work have resulted in massive amounts of errors.

Thanks in advance for any help!

here is the full code:

// arrays
var myCams : Camera[];
var mySeats : Transform[];

// timer
var timer : int = 30;

// ship
var ship : GameObject;

// active control vars
private var state : int = -1;
private var count : int = 0;

// player position & cam
var player_object : Transform;
var player_camera : Camera;

function Update()
{
    // fix speed
    timer = timer - 1;

    if (Input.GetAxis ("e_key") && state == -1 && timer < 0)
    {
        for (var thisSeat in mySeats)
        {
            // add to count so we know what seat to broadcast
            count = count + 1;

            // test for proximity to a seat
            if(Vector3.Distance (thisSeat.position, player_object.position ) < 3 )
            {
                // deactivate first person camera
                player_camera.GetComponent("Camera").active = false;

                // activate other camera
                myCams[thisSeat].GetComponent("Camera").active = true;

                // now sitting at
                state = count - 1;

                // tell turret / driving scripts they can activate
                BroadcastMessage ("Activate_Turret", state);
            }
        }

        // reset count
        count = -1;
    }
    if (Input.GetAxis ("e_key") && state > -1 && timer < 0)
    {
        // we are now first person again
        state = -1;

        // reset timer
        timer = 30;

        // tell turret / driving scripts they can deactivate
        BroadcastMessage ("Activate_Turret", -1);

        // reactivare first person camera
        player_camera.GetComponent("Camera").active = true;

        // disable all other cameras
        for (var thisCam in myCams)
        {
            thisCam.GetComponent("Camera").active = false;
        }
    }
}    

Your "for loop" is a so called "for each" loop. The loop var thisSeat receives the current element of the array. Since the mySeats array is of type Transform thisSeat is also a Transform.

You should use typed variables, because they are faster and it’s clearer what they actually do.

for (var thisSeat : Transform in mySeats)

Ok, that's was the first thing i've seen. Now your "real" problem:

You try to use a variable of type Transform as an int index into the cam-array. That’s impossible :wink:

For each loops are quite handy because you don't have to deal with index variables and so on, but inside the loop you can't determine the actual index of the current element. You can always just iterate through one array at a time.

You have to do it the "old" traditional way ;)

( not 100% sure whether the syntax is correct or not, i’m not a JS user :stuck_out_tongue: )

for (var index : int = 0; index < mySeats.Length; index++)
{
    if(Vector3.Distance (mySeats[index].position, player_object.position ) < 3 )
    {
        // deactivate first person camera
        player_camera.camera.active = false;
        // activate other camera
        myCams[index].camera.active = true;
        // now sitting at
        state = index;
        // tell turret / driving scripts they can activate
        BroadcastMessage ("Activate_Turret", state);
        // break out of the for loop cause we have already selected one seat
        break;
    }
}

// arrays
var myCams : Camera[];
var mySeats : Transform[];

// timer
var timer : int = 3;

// ship
var ship : GameObject;

// active control vars
private var state : int = -1;
private var count : int = 0;

// player position & cam
var player_object : Transform;
var player_camera : Camera;
var old_position : Vector3;

function Update()
{

    if (Input.GetAxis ("e_key"))
    {

        if (state == -1 && timer < 0)
        {

            for (var index : int = 0; index < mySeats.Length; index++)
            {

                if(Vector3.Distance (mySeats[index].position, player_object.position ) < 3 )
                {
                    // deactivate first person camera
                    player_camera.enabled = false;

                    // activate other camera
                    myCams[index].enabled = true;

                    // now sitting at
                    state = index;

                    print (index);

                    // tell turret / driving scripts they can activate
                    BroadcastMessage ("Activate_Turret", state);

                    // reset timer
                    timer = 20;

                    // parent user to seat
                    player_object.parent = mySeats[index];

                    old_position = player_object.transform.localPosition;

                    player_object.transform.localPosition = Vector3(0, 0, 0);

                    // break out of the for loop cause we have already selected one seat
                    break;
                }
            }
        }

        // get out
        if (state > -1 && timer < 0)
        {
            // we are now first person again
            state = -1;

            // reset timer
            timer = 30;

            // tell turret / driving scripts they can deactivate
            BroadcastMessage ("Activate_Turret", -1);

            // put back relitave to where the player entered the vehicle/seat
            player_object.transform.localPosition = old_position;

            // Detaches the transform from its parent.
            transform.parent = null;

            // reactivare first person camera
            player_camera.GetComponent("Camera").enabled = true;

            // disable all other cameras
            for (var thisCam in myCams)
            {
                thisCam.GetComponent("Camera").enabled = false;
            }
        }
    }

    // fix speed
    timer = timer - 1;
}