I get this error: IndexOutOfRangeException: Array index is out of range.

Hi,

I keep getting this error message. I already used google to find a solution but they don`t seem to work out for me.
This is the error:

IndexOutOfRangeException: Array index is out of range.
SittingPeopleControlScript.Update () (at Assets/SittingPeopleControlScript.js:32)

This is my code:

#pragma strict

var Player : Transform;

public var People : GameObject[];
var Individual = 0;

var PeopleScript : HumanSittingScript;
var SampleDis : float;

var Looping : boolean = true;


function Start () {

//Individual = Random.Range(0,People.length);
Individual = 0;
}

function Update () {

    //Assign Instances

    if (Looping)
    {
        //next individual
        if (Individual > (People.length-1)) {Individual = 0;}else{Individual += 1;}
       
        //Return variables first
        PeopleScript = People[Individual].GetComponent(HumanSittingScript);
        SampleDis = Vector3.Distance(People[Individual].transform.position, Player.position);
       
        if (SampleDis > 75)
            {
                if (People[Individual].active)
                    {
                    PeopleScript.Respawn();
                    }
            }
            else
            {
                if (!People[Individual].active)
                    {
                    People[Individual].active = true;
                    PeopleScript.Initiate ();
                    }
            }
    }   
   
   
}

Can anybody help me?

You have a logic error in your if statement on line 27.
I suggestion, set a breackpoint and step thru your code inspecting the variable Individual.

Excuse me, but I dont really get what youre saying.

if (Individual > (People.length-1)) {Individual = 0;}else{Individual += 1;}

This line can set the index to the value which is equal to the length of the array because you increment the ‘individual’ variable although it may already be the highest value which is valid as an index in this context.

Example:

  • length is 10, length -1 = 9
  • individual is already 9

9>9 => false, the else-part is run , individual will be incremented to 10, afterwards you would try to access People[10] in this given example which is an invalid index.

1 Like

I upgraded a project from 4.5 to 4.6 and also see this error spammed in the editor:
IndexOutOfRangeException: Array index is out of range.
SyntaxTree.VisualStudio.Unity.Bridge.WinPath.Combine (System.String left, System.String right)
SyntaxTree.VisualStudio.Unity.Bridge.UnityProject.Folders ()
SyntaxTree.VisualStudio.Unity.Bridge.UnityProject+<>c__DisplayClass18.b__15 ()
SyntaxTree.VisualStudio.Unity.Bridge.UnityProject.ItemGroup (System.IO.TextWriter w, System.Action a)
SyntaxTree.VisualStudio.Unity.Bridge.UnityProject.Serialize (System.IO.TextWriter w)
SyntaxTree.VisualStudio.Unity.Bridge.ProjectFile.WriteIfNecessary (SyntaxTree.VisualStudio.Unity.Bridge.FileGenerationHandler generationHandler)
SyntaxTree.VisualStudio.Unity.Bridge.ProjectFilesGenerator.GenerateProjectFiles ()
SyntaxTree.VisualStudio.Unity.Bridge.ProjectFilesGenerator+<>c__DisplayClass1.<.cctor>b__0 ()
UnityEditor.EditorApplication.Internal_CallUpdateFunctions ()

UPDATE:
Looks like UnityVS needed to be reimported.