What did I do wrong with declaring the array? (Fixed)

Greetings,

This is fixed, see for the code at the bottom.

I had an issue. It had to do with the following code:

 

var camver : int;
/// To recall at what camera position it is right now, and to which one it has to go.
var timesensitivity : float;
/// How fast the timer goes.
var timetaker : float;

var campositions : Vector3[] = new Vector3[4];
var camrotation : Vector3[] = new Vector3[4];

function Start () {

camver = 0;
timesensitivity = 1;
timetaker = 4;

campositions[0] = Vector3(38,5,67);
campositions[1] = Vector3(43,3,78);
campositions[2] = Vector3(56,2,64);
campositions[3] = Vector3(44,1,64);

camrotation[0] = Vector3(24, 50, 350);
camrotation[1] = Vector3(4,50,-1);
camrotation[2] = Vector3(4,160,4);
camrotation[3] = Vector3(-8, 100, -3);

}

function Update () {

if(timetaker > 0) {
timetaker -= timesensitivity * Time.deltaTime;
	} else {
	
	camver += 1;
	timetaker = 15;
	transform.position = campositions[camver];
	transform.Rotate(camrotation[camver]);
	
	
	}
	if (camver >= 3){
	camver = 0;
	}
}

Now, these are the errors:


IndexOutOfRangeException: Array index is out of range. > Fixed
CameraRotation.Start () (at Assets/Menu/Scripts/CameraRotation.js:17) > Fixed

&

IndexOutOfRangeException: Array index is out of range. > Not Fixed
CameraRotation.Update () (at Assets/Menu/Scripts/CameraRotation.js:38) > Not Fixed

What the code is supposed to do, is use the arrays to quickly switch positions & rotations every 15 seconds, the timetaker, to the desired position & rotation, using camver to get to the right part of the array.

I’ve tried some things, and they all gave me an error, this is the code I have now.

Now, the question is, is what did I do wrong with declaring the array (or something else?), and how can I fix this?
And, is there a way to make the code more efficient, because I think arrays are (in this situation) the most efficient approach.

Any help would be greatly appreciated!

Jip

Edit: updated with the new script, same issue.

Edit: The proper script that works:
(PS: Note that the values of the arrays are given inside the inspector)


var camver : int;
/// To recall at what camera position it is right now, and to which one it has to go.
var timesensitivity : float;
/// How fast the timer goes.
var timetaker : float;
var timetakervalue : float;

var campositions : Vector3[];
var camrotation : Vector3[];

function Start () {
timetaker = timetakervalue;
}

function Update () {

if(timetaker > 0) {
timetaker -= timesensitivity * Time.deltaTime;
	} else {
	camver += 1;
	timetaker = 15;
	transform.position = campositions[camver];
	transform.Rotate(camrotation[camver]);
	}
	if (camver >= 3){
	camver = -1;
	}
}

You declare your arrays with a size of 3, but you try to store 4 values in them.

var campositions : Vector3[] = new Vector3[4];
var camrotation : Vector3[] = new Vector3[4];

You shouldn’t do this:

var campositions : Vector3[] = new Vector3[4];

and then add values in Start, because public variables always get their values from the inspector, not the code. This means that you can resize “campositions” in the inspector, so it’s not guaranteed to have 4 items in the array. If the variable should in fact be public, just do this:

var campositions : Vector3[];

and fill the values in with the inspector. Use campositions.Length instead of hard-coding a number, because it can change.

If the variable shouldn’t be public, then say so, and in this case you can supply values in code, since they don’t come from the inspector.

private var campositions = [Vector3(38,5,67), Vector3(43,3,78), Vector3(56,2,64), Vector3(44,1,64)];

However it’s still a good idea not to use hard-coded numbers for the array size, but still use campositions.Length, in case you change the array size later.