Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

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

Discussion in 'Scripting' started by Rob-Reijnen, Nov 17, 2014.

  1. Rob-Reijnen

    Rob-Reijnen

    Joined:
    Oct 14, 2013
    Posts:
    60
    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:
    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var Player : Transform;
    4.  
    5. public var People : GameObject[];
    6. var Individual = 0;
    7.  
    8. var PeopleScript : HumanSittingScript;
    9. var SampleDis : float;
    10.  
    11. var Looping : boolean = true;
    12.  
    13.  
    14. function Start () {
    15.  
    16. //Individual = Random.Range(0,People.length);
    17. Individual = 0;
    18. }
    19.  
    20. function Update () {
    21.  
    22.     //Assign Instances
    23.  
    24.     if (Looping)
    25.     {
    26.         //next individual
    27.         if (Individual > (People.length-1)) {Individual = 0;}else{Individual += 1;}
    28.        
    29.         //Return variables first
    30.         PeopleScript = People[Individual].GetComponent(HumanSittingScript);
    31.         SampleDis = Vector3.Distance(People[Individual].transform.position, Player.position);
    32.        
    33.         if (SampleDis > 75)
    34.             {
    35.                 if (People[Individual].active)
    36.                     {
    37.                     PeopleScript.Respawn();
    38.                     }
    39.             }
    40.             else
    41.             {
    42.                 if (!People[Individual].active)
    43.                     {
    44.                     People[Individual].active = true;
    45.                     PeopleScript.Initiate ();
    46.                     }
    47.             }
    48.     }  
    49.    
    50.    
    51. }
    Can anybody help me?
     
  2. Deleted User

    Deleted User

    Guest

    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.
     
  3. Rob-Reijnen

    Rob-Reijnen

    Joined:
    Oct 14, 2013
    Posts:
    60
    Excuse me, but I don`t really get what you`re saying.
     
  4. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Code (JavaScript):
    1. 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.
     
    image28 likes this.
  5. dustinandrew

    dustinandrew

    Joined:
    Apr 30, 2010
    Posts:
    102
    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.<Serialize>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.
     
    Last edited: Dec 2, 2014