Search Unity

[SOLVED] Index was outside the bounds of the array ??

Discussion in 'Scripting' started by Quast, Jun 1, 2019.

  1. Quast

    Quast

    Joined:
    Jul 5, 2015
    Posts:
    560
    I'm using array to insert 4 elements that left hand of my character will move between theme one by one. The problem is that it tells me "IndexOutOfRangeException: Index was outside the bounds of the array." !!
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Iks: MonoBehaviour
    6. {
    7.  
    8.     public clickme _active;
    9.     public bool reload;
    10.     public int currt;
    11.     public float dis;
    12.     public Transform[] reloadPositions; // 4 elements
    13.  
    14.  
    15.     void Start()
    16.     {
    17.  
    18.         foreach (Transform trans in gameObject.GetComponentsInChildren<Transform>())
    19.         {
    20.  
    21.             if (trans.name == "set")
    22.             {
    23.                 reloadPositions[3] = trans.gameObject.transform;
    24.             }
    25.             if (trans.name == "set (1)")
    26.             {
    27.                 reloadPositions[0] = trans.gameObject.transform;
    28.             }
    29.             if (trans.name == "set (2)")
    30.             {
    31.                 reloadPositions[1] = trans.gameObject.transform;
    32.             }
    33.             if (trans.name == "set (3)")
    34.             {
    35.                 reloadPositions[2] = trans.gameObject.transform;
    36.             }
    37.         }
    38.        
    39.  
    40.     } // end start
    41.  
    42.  
    43.     void Update()
    44.     {
    45.  
    46.  
    47.         currt = reloadPositions.Length;
    48.         if (reload == true && _active.grenade == false)
    49.         {
    50.             dis = Vector3.Distance(_active.leftHandObj.transform.localPosition, reloadPositions[currt].transform.localPosition); // error in this line
    51.             if (dis < 0.1)
    52.             {
    53.                 currt = currt + 1;
    54.                 if (currt >= reloadPositions.Length && dis < 1)
    55.                 {
    56.                     currt = reloadPositions.Length - 1;
    57.  
    58.                     reload = false;
    59.                     currt = 0;
    60.  
    61.                 }
    62.             }
    63.             _active.leftHandObj.transform.localPosition = Vector3.MoveTowards(_active.leftHandObj.transform.localPosition, reloadPositions[currt].localPosition, Time.deltaTime / 1.0f);
    64.  
    65.         }
    66.         else
    67.         {
    68.         //    _active.leftHandObj.transform.localPosition = Vector3.MoveTowards(_active.leftHandObj.transform.localPosition, reloadPositions[reloadPositions.Length - 1].localPosition, Time.deltaTime / 1.0f);
    69.         }
    70.  
    71.  
    72.     } // end update
    73.  
    74.  
    75. }
    76.  
     
  2. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,429
    You're not saying which line threw the error, but I would bet it is line 23. If you haven't filled the list before
    Start
    , then there is no
    [3]
    in the array.
     
  3. Deleted User

    Deleted User

    Guest

    Did you define them in the Inspector?
     
    LordHeman likes this.
  4. Quast

    Quast

    Joined:
    Jul 5, 2015
    Posts:
    560
    I mention it on the end of line 50.
    Yes. 4 elements.

    Update:
    Line 47, should be deleted ^-^'

    Thank you guys.
     
    Last edited: Jun 1, 2019
  5. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,429
    Sorry, I didn't see the comment buried to the right.

    Above line 50, you set
    currt
    to be the length of the array. Then you access the
    reloadPositions[currt]
    which is past the end.

    If an array's length is 5, then the valid index positions in the array are 0 1 2 3 4, and never 5. Think of it as [ 0, 5 ) or "zero through almost 5."
     
    Quast likes this.
  6. Quast

    Quast

    Joined:
    Jul 5, 2015
    Posts:
    560
    It's OK.
    Thank you.