keep getting errors in this script
error = IndexOutOfRangeException: Array index is out of range.
CharacterController_2D.Update () (at Assets/Chapter 1/Scripts/CharacterController_2D.js:85)
public var f_speed : float = 5.0;
public var loopSprites : SpriteManager[];
public var jumpSprite : JumpSpriteManager;
public var jumpSound : AudioClip;
public var layerMask : LayerMask;
var Renderer = true;
var customBox : GUIStyle;
var customlives : GUIStyle;
var CustomLevelBox : GUIStyle;
var loseLife = false;
private var restartButton : GUITexture;
private var in_direction : int;
private var b_isJumping : boolean;
private var f_height : float;
private var f_lastY : float;
private var b_hasKey : boolean;
var lives :int = 3;
var originalWidth = 800.0; // define here the original resolution
var originalHeight = 480.0; // you used to create the GUI contents
private var scale: Vector3;
public var myJoystick : VCAnalogJoystickBase; // assign this via the inspector
public var myButton : VCButtonBase; // assign this via the inspector
private var mesh:Mesh;
function FixedUpdate () {
//rigidbody.AddForce (Vector3.forward * myJoystick.AxisY * speed * Time.deltaTime);
//rigidbody.AddForce (Vector3.right * myJoystick.AxisX * speed * Time.deltaTime);;
}
public function Start() : void {
//Get restartButton from the Game Scene
restartButton = GameObject.FindWithTag("RestartButton").guiTexture;
//make restart Button disabled
restartButton.enabled = false;
//Start with no Key
//b_hasKey = false;
//Get mesh from the character MeshFilter
mesh = GetComponent(MeshFilter).sharedMesh;
//Get hight from the top of our character to the bottom of our character
f_height = mesh.bounds.size.y * transform.localScale.y;
//Set up the last y-axis position of our character
f_lastY = transform.position.y;
//b_isJumping = false;
in_direction = 1;
//Initialization Sprite Manager
for (var i : int = 0; i < loopSprites.length; i++) {
loopSprites[i].init();
}
//Update Camera to the character position
Camera.main.transform.position = new Vector3(transform.position.x, transform.position.y, Camera.main.transform.position.z);
}
// Update is called once per frame
public function Update () : void {
//If our character isn’t jumping
if (!b_isJumping) {
if (myJoystick.AxisX > 0.0f || myJoystick.AxisX < 0.0f){
//if (Input.GetButton("Horizontal")) {
//Walking
in_direction = myJoystick.AxisX < 0.0f ? -1 : 1;
//in_direction = Input.GetAxis("Horizontal") < 0 ? -1 : 1;
rigidbody.velocity = new Vector3((in_direction*f_speed), rigidbody.velocity.y, 0);
//Reset Stay animation frame back to the first frame
loopSprites[0].resetFrame();
//Update Walking animation while the character is walking
loopSprites[1].updateAnimation(in_direction, renderer.material);
} else {
//Stay
//Reset Walking animation frame back to the first frame
loopSprites[1].resetFrame();
//Update Stay animation while the character is not walking
loopSprites[0].updateAnimation(in_direction, renderer.material);
}
//Jump
//if (Input.GetButton("Jump")) {
if (myButton.Pressed) {
b_isJumping = true;
//Then make it Jump
audio.volume = 0.3;
audio.PlayOneShot(jumpSound);
loopSprites[0].resetFrame();
loopSprites[1].resetFrame();
rigidbody.velocity = new Vector3(rigidbody.velocity.x, -Physics.gravity.y, 0);
}
} else {
//update animation while it Jump
jumpSprite.updateJumpAnimation(in_direction, rigidbody.velocity.y, renderer.material);
}
}
public function LateUpdate() : void {
//Checking Jumping by using Raycast
var hit : RaycastHit;
var v3_hit : Vector3 = transform.TransformDirection (-Vector3.up) * (f_height * 0.5);
var v3_right : Vector3 = new Vector3(transform.position.x + (collider.bounds.size.x*0.45), transform.position.y, transform.position.z);
var v3_left : Vector3 = new Vector3(transform.position.x - (collider.bounds.size.x*0.45), transform.position.y, transform.position.z);
if (Physics.Raycast (transform.position, v3_hit, hit, 2.5, layerMask.value)) {
b_isJumping = false;
} else if (Physics.Raycast (v3_right, v3_hit, hit, 2.5, layerMask.value)) {
if (b_isJumping) {
b_isJumping = false;
}
} else if (Physics.Raycast (v3_left, v3_hit, hit, 2.5, layerMask.value)) {
if (b_isJumping) {
b_isJumping = false;
}
} else {
if (!b_isJumping) {
if (Mathf.Floor(transform.position.y) == f_lastY) {
b_isJumping = false;
} else {
b_isJumping = true;
}
}
}
f_lastY = Mathf.Floor(transform.position.y);
//Update Camera
Camera.main.transform.position = new Vector3(transform.position.x, transform.position.y, Camera.main.transform.position.z);
}
public function OnTriggerEnter (hit : Collider) : IEnumerator {
}
public function OnDrawGizmos() : void {
mesh = GetComponent(MeshFilter).sharedMesh;
f_height = mesh.bounds.size.y * transform.localScale.y;
var v3_right : Vector3 = new Vector3(transform.position.x + (collider.bounds.size.x*0.45), transform.position.y, transform.position.z);
var v3_left : Vector3 = new Vector3(transform.position.x - (collider.bounds.size.x*0.45), transform.position.y, transform.position.z);
Gizmos.color = Color.red;
Gizmos.DrawRay(transform.position, transform.TransformDirection (-Vector3.up) * (f_height * 0.5));
Gizmos.DrawRay(v3_right, transform.TransformDirection (-Vector3.up) * (f_height * 0.5));
Gizmos.DrawRay(v3_left, transform.TransformDirection (-Vector3.up) * (f_height * 0.5));
}
class SpriteManager {
public var spriteTexture : Texture2D; //Set Texture use for a loop animation such as walking, stay, etc.
public var in_framePerSec : int; //Get frame per sec to calculate time
public var in_gridX : int; //Get max number of Horizontal images
public var in_gridY : int; //Get max number of Vertical images
private var f_timePercent : float;
private var f_nextTime : float; //Update time by using frame persecond
private var f_gridX : float;
private var f_gridY : float;
private var in_curFrame : int;
public function init () : void {
f_timePercent = 1.0/in_framePerSec;
f_nextTime = f_timePercent; //Update time by using frame persecond
f_gridX = 1.0/in_gridX;
f_gridY = 1.0/in_gridY;
in_curFrame = 1;
}
public function updateAnimation (_direction : int, _material : Material) : void {
//Update material
_material.mainTexture = spriteTexture;
//Update frame by time
if (Time.time > f_nextTime) {
f_nextTime = Time.time + f_timePercent;
in_curFrame++;
if (in_curFrame > in_framePerSec) {
in_curFrame = 1;
}
}
_material.mainTextureScale = new Vector2 (_direction * f_gridX, f_gridY);
var in_col : int = 0;
if (in_gridY > 1) {
//If there is more than one grid on the y-axis update the texture
in_col = Mathf.Ceil(in_curFrame/in_gridX);
}
if (_direction == 1) { //Right
_material.mainTextureOffset = new Vector2(((in_curFrame)%in_gridX) * f_gridX, in_col*f_gridY);
} else { //Left
//Flip Texture
_material.mainTextureOffset = new Vector2(((in_gridX + (in_curFrame)%in_gridX)) * f_gridX, in_col*f_gridY);
}
}
public function resetFrame () :void {
in_curFrame = 1;
}
}
class JumpSpriteManager {
public var t_jumpStartTexture : Texture2D; //Alternative Jump Texture play after t_jumpReadyTextures
public var t_jumpAirTexture : Texture2D; //Alternative Jump Texture play when the player in the air at the top position of projectile
public var t_jumpDownTexture : Texture2D; //Alternative Jump Texture play when the player fall to the ground
public function updateJumpAnimation (_direction : int, _velocityY : float, _material : Material) : void {
//Checking for the player position in the air
if ((_velocityY >= -2.0) (_velocityY <= 2.0)) { //Top of the projectile
_material.mainTexture = t_jumpAirTexture;
} else if (_velocityY > 2.0) { //Start Jump
_material.mainTexture = t_jumpStartTexture;
} else { //Fall
_material.mainTexture = t_jumpDownTexture;
}
_material.mainTextureScale = new Vector2 (_direction * 1, 1);
_material.mainTextureOffset = new Vector2 (_direction * 1, 1);
}
}
function OnGUI () {
scale.x = Screen.width/originalWidth; // calculate hor scale
scale.y = Screen.height/originalHeight; // calculate vert scale
scale.z = 1;
var svMat = GUI.matrix; // save current matrix
// substitute matrix - only scale is altered from standard
GUI.matrix = Matrix4x4.TRS(Vector3.zero, Quaternion.identity, scale);
GUI.matrix = Matrix4x4.TRS(Vector3.zero, Quaternion.identity, scale);
GUI.Box (Rect (200, 10, 190, 20), "",CustomLevelBox);
GUI.Label(Rect(100, 10, 50, 20), "" + lives,customlives);
if (lives == 0)
{
//yield WaitForSeconds ( 0.5 );
renderer.enabled = true;
loseLife = false;
Application.LoadLevel("Main Menu");
}
// restore matrix before returning
GUI.matrix = svMat; // restore matrix
}
@script RequireComponent (AudioSource)
need help asap thanks