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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

ArgumentOutOfRangeException: Argument is out of range - Can't fix need help :(

Discussion in 'Scripting' started by Jivecookie, Oct 1, 2015.

  1. Jivecookie

    Jivecookie

    Joined:
    Sep 27, 2015
    Posts:
    4
    Hey all so I am undergoing the error as shown in the title. I can't seem to solve it as I am not even sure as to why it is happening, anyways here's the code creating the error.

    The error occurs apparently at line which states:

    Code (CSharp):
    1. diff = transform2[i + 1] - transform2[i];


    Code (CSharp):
    1. public class xrotationcapsule : MonoBehaviour {
    2.  
    3.     public Quaternion p;
    4.     public Quaternion t;
    5.     float cheekypoop = 90;
    6.     public Vector3 capsulemove;
    7.     int count1 = 0;
    8.     int count2 = 0;
    9.     int count3 = 0;
    10.     float pizza = 0.0f;
    11.     float diff;
    12.     float diff2;
    13.     string str;
    14.  
    15.     List<float> transform2;
    16.  
    17.     // Use this for initialization
    18.     void Start () {
    19.  
    20.         capsulemove = this.gameObject.transform.position;
    21.  
    22.         transform2 = new List<float> ();
    23.  
    24.  
    25.         InvokeRepeating ("capsulerotation", 0f, 0.0001f);
    26.      
    27.     }
    28.    
    Code (CSharp):
    1. void capsulerotation()
    2.     {
    3.  
    4.  
    5.         for (int i = 0; i<transform2.Count; i++) {
    6.  
    7.  
    8.             diff = transform2[i + 1] - transform2[i];
    9.  
    10.             if (diff >  0.015f)
    11.             {
    12.  
    13.                 str =  "left";
    14.  
    15.              
    16.  
    17.  
    18.             }
    19.             else if (diff < - 0.015f)
    20.             {
    21.  
    22.                 str = "right";
    23.          
    24.  
    25.          
    26.             }
    27.  
    28.             else if (diff == 0){
    29.  
    30.                 str = "middle";
    31.  
    32.                 if ( cheekypoop < 90 )
    33.                 {
    34.                     cheekypoop += 8.000f;
    35.  
    36.                 }
    37.  
    38.                 if ( cheekypoop > 90 )
    39.                 {
    40.                     cheekypoop -= 8.000f;
    41.                 }
    42.  
    43.  
    44.      
    45.             }
    46.  
    47.  
    48.  
    49.  
    50.         }
    51.  
    Thanks in advance to those who help me out :)
     
    Last edited: Oct 1, 2015
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,745
    You're looping through an array that goes from (let's say) 0 to 15. In the last loop, i will be 15, so you're then trying to access [i + 1], or index 16 - which doesn't exist. You could make it only loop to transform2.Count - 1.
     
    Jivecookie likes this.
  3. Jivecookie

    Jivecookie

    Joined:
    Sep 27, 2015
    Posts:
    4
    Hey this was my assumption at first, but then I tried
    Code (CSharp):
    1. diff = transform2[i] - transform2[i - 1];
    which logically, would work as, as far as I know it means, transform[index(15)] - transform[index(14)] right?

    but no I still got the exact same error strangely enough.
     
  4. Jivecookie

    Jivecookie

    Joined:
    Sep 27, 2015
    Posts:
    4
    Nvm StarManta, thanks for the help I solved it by doing..

    diff = transform2 - transform2[transform2.Count - 3];

    I did -3 rather than -1 as transform2.Count is just a bit quicker updating than 'i' itself.

    Thanks.