Array Index out of Range?

Ok so I’m using this for statement. And whenever I run the game it says “IndexOutOfRangeException: Array index is out of range.” Is there a problem with my code? or is it something else?

for (var i = 0; i < count; i++){
var touch : iPhoneTouch = iPhoneInput.touches[i];

Without knowing what count equals, nobody can help you. The problem you’re having is explained in the error message you’re getting - you’re trying to access an array element that’s out of range, ie you have an array with 10 elements and you’re trying to access number 50 or something.

Also, your code as you’ve posted doesn’t really make a lot of sense. Not only that, but you probably don’t want to be declaring variables inside a for loop.

well there’s more to the code, it’s just that its huge. That particular bit of code makes it so I can get the iphone touches, but the array is out of range or whatever so it doesn’t work when I tap the buttons on my iphone. count equals 4, and I have it so that if “i” equals 5 it’s supposed to go back to 1. Here’s the rest of the code.

var pauseMenu : GameObject;
var FB : GUITexture;
var RB : GUITexture;
var PB : GUITexture;
var SB : GUITexture;
var limitedAmmo : float;
var AmmoLeft : float = 100.0;
var reload : boolean = false;
var count : float = 4.0;

var WC : int = 1;
var S : GameObject;
var HMG : GameObject;
var AR : GameObject;
var RPG : GameObject;
var SIcon : GameObject;
var HMGIcon : GameObject;
var RPGIcon : GameObject;
var ARIcon : GameObject;

var shoot : boolean = true;
var projectile : Rigidbody;
var bulletSpeed : float = 20;
var direction : float = 1;
var fromPosition : Transform;

var ARD : Transform;
var HMGD : Transform;
var RPGD : Transform;
var SD : Transform;

function Start () {
S = gameObject.FindWithTag("Sniper");
HMG = gameObject.FindWithTag("HMG");
AR = gameObject.FindWithTag("Assault");
RPG = gameObject.FindWithTag("RPG");

HMG.SetActiveRecursively(false);
HMGIcon.SetActiveRecursively(false);
RPG.SetActiveRecursively(false);
RPGIcon.SetActiveRecursively(false);
S.SetActiveRecursively(false);
SIcon.SetActiveRecursively(false);
}

function Update () {
for (var i = 0; i < count; i++){
var touch : iPhoneTouch = iPhoneInput.touches[i];
var FBHit = FB.HitTest(touch.position);
var RBHit = RB.HitTest(touch.position);
var PBHit = PB.HitTest(touch.position);
var SBHit = SB.HitTest(touch.position);
}

if(i == 4){
i = 1;
}

if (PBHit){
if (Time.timeScale == 0.0){
Time.timeScale = 1.0;
//pauseMenu.active = false;
}
else {
Time.timeScale = 0.0;
//pauseMenu.active = true;
}
}


if(limitedAmmo < 10){
if(AmmoLeft > 0){
reload = true;
}
else{
reload = false;
}
}


if(RBHit){
if(reload){
limitedAmmo = 10.0;
AmmoLeft -= 10.0;
}
}

if(SBHit){
WeaponSwitch();
}


if(FBHit){
fireBullet();
WaitForSeconds(1);
}
}

function WeaponSwitch(){
if(WC == 4){
WC = 1;
}
else{
WC ++;
Debug.Log("Weapon Switch Number" + WC);
}
if(WC == 1){
fromPosition = ARD;
AR.SetActiveRecursively(true);
ARIcon.SetActiveRecursively(true);
S.SetActiveRecursively(false);
SIcon.SetActiveRecursively(false);
}
if(WC == 2){
fromPosition = RPGD;
RPG.SetActiveRecursively(true);
RPGIcon.SetActiveRecursively(true);
AR.SetActiveRecursively(false);
ARIcon.SetActiveRecursively(false);
}
if(WC == 3){
fromPosition = HMGD;
HMG.SetActiveRecursively(true);
HMGIcon.SetActiveRecursively(true);
RPG.SetActiveRecursively(false);
RPGIcon.SetActiveRecursively(false);
}
if(WC == 4){
fromPosition = SD;
S.SetActiveRecursively(true);
SIcon.SetActiveRecursively(true);
HMG.SetActiveRecursively(false);
HMGIcon.SetActiveRecursively(false);
}
}

function fireBullet(){
          var instantiatedProjectile:Rigidbody =
Instantiate(projectile, fromPosition.position, fromPosition.rotation);
instantiatedProjectile.velocity = transform.forward * bulletSpeed * direction;

Physics.IgnoreCollision(instantiatedProjectile.collider, transform.root.collider);
}

I don’t really have the time to read all that code and I know nothing about iphone development, but I do know that you have a whole crapload of “ifs” in there that suggest you might want to rethink this code and perhaps even learn a bit more about scripting at a basic level.

I would start by asking yourself if count should really be a float, and what exactly you expect that Update function to be doing apart from overwriting those local variables you’re declaring repeatedly. You don’t need to declare ANY variables inside this update function.

Again, the error message is telling you what’s wrong - you’re going outside the range of your array, in this case iPhoneInput.touches[ ], whatever that is.

Well yes I realize that it’s telling me what’s wrong, but I don’t know how to fix it. I don’t really see anyway of doing it without the if statements, they all seem necessary to me.

You need to understand how For loops work, how to combine If statements using logic operators such as “and” and “or”, and how to replace 90% of them with a Switch statement. Your counter variable (i) is initialised within the for loop and yet you’re trying to manipulate it from outside. It will never increment beyond the limits you’ve set, and yet you’re trying to access it and reset it to 1, when (even if this worked or made any sense) you would want to reset it to 0.

The way to fix your problem, re: trying to access elements of the array that don’t exist is to not try to access them in the first place. If there are only 2 elements in this array, you can only access 2 of them, not 4.

Take a look at something like http://www.unitylabs.net/tutorials/unity-beginners/unity-scripting-2 , or whatever basic scripting tutorials Unity themselves offer in the documentation.

whaou impressive if statements ^^ ,

I agree with xomg , you should really get rid of them using switch instead , that will be more clear and also more easy to maintain, or you will get lost quickly, which is certainly the case with your array error already.

Ok I’ll see about getting rid of the if’s and using switch statements

As stated you really need to think about what you’re trying to achieve.
But with reference to this bit of code…

function Update () {
for (var i = 0; i < count; i++){
var touch : iPhoneTouch = iPhoneInput.touches[i];
var FBHit = FB.HitTest(touch.position);
var RBHit = RB.HitTest(touch.position);
var PBHit = PB.HitTest(touch.position);
var SBHit = SB.HitTest(touch.position);
}

if(i == 4){
i = 1;
}

First - change count to be an integer.
Second - “if (i==4)” would be true regardless because you never break out of the for loop at any time before it reaches count.
Third - defining the variables in the for loop will make them local to the loop so probably not visible outside of it.
Fourth - maybe find a basic programming reference and do some background investigation about lops (for, while) and logic statements (if, switch).

All I was trying to accomplish with the for statement was to be able to no matter what touch count it was, was to always be able to hit buttons, like with my old version of the game I could either hit the buttons only when one finger was touching or only when two were. Thanks for all the help :slight_smile: