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

ArgumentOutOfRangeException: Argument is out of range !

Discussion in 'Scripting' started by Xman236, Aug 8, 2018.

  1. Xman236

    Xman236

    Joined:
    Mar 5, 2018
    Posts:
    11
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5.  
    6. public class test1 : MonoBehaviour
    7. {
    8.  
    9.     private Collider2D auto1_collider;
    10.     private Collider2D auto2_collider;
    11.     public float speed;
    12.     public float moveX;
    13.     public float moveY;
    14.     public bool collied = false;
    15.     private bool key_pressed = false;
    16.     private Vector3 init_pos;
    17.     private GameObject wall_right;
    18.     private GameObject wall_left;
    19.     private GameObject wall_above;
    20.     private GameObject auto1;
    21.     private GameObject auto2;
    22.     private float dis_to_right_wall;
    23.     private float dis_to_left_wall;
    24.     private float dis_to_above_wall;
    25.     private float min_dis_to_wall;
    26.     private Vector3 targetPosition;
    27.     private float starttime;
    28.     private ColliderDistance2D collidist_left;
    29.     private ColliderDistance2D collidist_right;
    30.     private ColliderDistance2D collidist_above;
    31.  
    32.     ///
    33.     public int num_of_particles;
    34.     private GameObject[] particles_in_unity;
    35.     public Swarm my_swarm;
    36.     double minX;
    37.     double maxX;
    38.     double minY;
    39.     double maxY;
    40.  
    41.     double r1, r2; // cognitive and social randomizations
    42.     double[] newvelocity = new double[3];
    43.     double[] newposition = new double[3];
    44.     double w = 0.729; // inertia weight
    45.     double c1 = 1.49445; // cognitive/local weight
    46.     double c2 = 1.49445; // social/global weight
    47.  
    48.     public class Particle
    49.     {
    50.         public double error;
    51.         public double berror;
    52.         public int dim;
    53.         public double[] direction;
    54.         public double[] bdirection;
    55.         public double[] velocity;
    56.      
    57.  
    58.         public Particle(double[] dir, double[] bdir, double err, double berr, double[] vel)
    59.         {
    60.             this.direction = new double[dir.Length];
    61.             dir.CopyTo(this.direction, 0);
    62.             this.error = err;
    63.             this.velocity = new double[vel.Length];
    64.             vel.CopyTo(this.velocity, 0);
    65.             this.bdirection = new double[bdir.Length];
    66.             bdir.CopyTo(this.bdirection, 0);
    67.             this.berror = berr;
    68.          
    69.         }
    70.        
    71.     }
    72.  
    73.     public class Swarm
    74.     {
    75.  
    76.         public double[] gdirection;
    77.         public List<Particle> paricle_list;
    78.         public int num_of_particles;
    79.         public double gerror;
    80.  
    81.         public Swarm(int num_of_particles)
    82.         {
    83.             this.paricle_list  = new List<Particle>();
    84.             this.num_of_particles = num_of_particles;
    85.            
    86.             double[] tmp =  new double[3];
    87.            
    88.             for (int i = 0; i < num_of_particles; i++)
    89.             {
    90.                 this.paricle_list.Add(new Particle(new double[] { Random.Range(-5f,5f), Random.Range(-5f, 5f) , -0.2 }, tmp, 0, 0, tmp));
    91.             }
    92.             this.gdirection = new double[3];
    93.             tmp.CopyTo(this.gdirection, 0);
    94.             this.gerror = 0;
    95.  
    96.  
    97.         }
    98.     }
    99.  
    100.  
    101.  
    102.    
    103.  
    104.     // Use this for initialization
    105.     void Start()
    106.     {
    107.         particles_in_unity = GameObject.FindGameObjectsWithTag("particle");
    108.  
    109.         auto1 = GameObject.FindWithTag("particle1");
    110.         auto1_collider = auto1.GetComponent<Collider2D>();
    111.      
    112.         wall_right = GameObject.FindWithTag("wall_right");
    113.         wall_left = GameObject.FindWithTag("wall_left");
    114.         wall_above = GameObject.FindWithTag("wall_above");
    115.  
    116.         /// set min and max value
    117.         minX = wall_left.transform.position[0];
    118.         maxX = wall_right.transform.position[0];
    119.         maxY = wall_above.transform.position[1];
    120.         minY = -999;
    121.         ///
    122.  
    123.         init_pos = auto1.transform.position;
    124.         targetPosition = init_pos;
    125.  
    126.         starttime = Time.time;
    127.  
    128.         num_of_particles = 1;
    129.         my_swarm = new Swarm(num_of_particles);
    130.  
    131.     }
    132.  
    133.     // Update is called once per frame
    134.  
    135.        
    136.     void FixedUpdate()
    137.     {
    138.  
    139.         Distance_to_wall();
    140.         MoveIt();
    141.         float tmp;
    142.         tmp = Time.time - starttime;
    143.  
    144.  
    145.  
    146.         //Debug.Log("distance  " + min_dis_to_wall);
    147.         //Debug.Log("best survived time" + my_swarm.gerror);
    148.     }
    149.     void update_swarm(int particle_number)
    150.     {
    151.        
    152.         my_swarm.paricle_list[particle_number].error = Time.time;
    153.  
    154.  
    155.         //new velocity
    156.         for (int j = 0; j < my_swarm.paricle_list[particle_number].velocity.Length; ++j) // each component of the velocity
    157.         {
    158.             r1 = Random.Range(0, 1f);
    159.             r2 = Random.Range(0, 1f);
    160.  
    161.             newvelocity[j] = (w * my_swarm.paricle_list[particle_number].velocity[j]) +
    162.                 (c1 * r1 * (my_swarm.paricle_list[particle_number].bdirection[j] - my_swarm.paricle_list[particle_number].direction[j])) +
    163.                 (c2 * r2 * (my_swarm.gdirection[j] - my_swarm.paricle_list[particle_number].direction[j]));
    164.         }
    165.         newvelocity.CopyTo(my_swarm.paricle_list[particle_number].velocity, 0);
    166.  
    167.         // new position
    168.         for (int j = 0; j < my_swarm.paricle_list[particle_number].direction.Length; ++j)
    169.         {
    170.             newposition[j] = my_swarm.paricle_list[particle_number].direction[j] + newvelocity[j];
    171.  
    172.             // check for the max and min values
    173.             if (newposition[0] < minX)
    174.                 newposition[0] = minX;
    175.             else if (newposition[0] > maxX)
    176.                 newposition[0] = maxX;
    177.  
    178.             if (newposition[1] < minY)
    179.                 newposition[1] = minY;
    180.             else if (newposition[1] > maxY)
    181.                 newposition[1] = maxY;
    182.             //
    183.         }
    184.  
    185.         if (my_swarm.paricle_list[particle_number].error > my_swarm.paricle_list[particle_number].berror)
    186.         {
    187.             my_swarm.paricle_list[particle_number].berror = my_swarm.paricle_list[particle_number].error;
    188.  
    189.         }
    190.         if (my_swarm.paricle_list[particle_number].berror > my_swarm.gerror)
    191.         {
    192.             my_swarm.gerror = my_swarm.paricle_list[particle_number].berror;
    193.  
    194.         }
    195.  
    196.         newposition.CopyTo(my_swarm.paricle_list[particle_number].direction, 0);
    197.  
    198.  
    199.     }
    200.  
    201.  
    202.     void MoveIt()
    203.     {
    204.         var startPosition = auto1.transform.position;
    205.         float step = speed * Time.deltaTime;
    206.  
    207.         if (startPosition != targetPosition)
    208.         {
    209.            
    210.             auto1.transform.position = Vector3.MoveTowards(startPosition, targetPosition, step);
    211.            
    212.             //if the wall is reached, reset
    213.             if (collied == true)
    214.             {
    215.                
    216.  
    217.                 targetPosition = init_pos;
    218.                 auto1.transform.position = targetPosition;
    219.                 collied = false;
    220.  
    221.                 update_swarm(num_of_particles);
    222.                 starttime = Time.time;
    223.  
    224.  
    225.  
    226.                
    227.                 return;
    228.             }  
    229.         }
    230.  
    231.         else
    232.         {
    233.             //targetPosition = new Vector3((float)my_swarm.paricle_list[0].direction[0], (float)my_swarm.paricle_list[0].direction[1], init_pos[2]);
    234.             targetPosition = new Vector3(Random.Range(-5f, 5f), Random.Range(-5f, 5f), init_pos[2]);
    235.         }
    236.  
    237.     }
    238.     void Distance_to_wall()
    239.     {
    240.  
    241.         collidist_right = auto1_collider.Distance(wall_right.GetComponent<Collider2D>());
    242.         //Debug.Log("Dist_right" + collidist_right.distance);
    243.         collidist_left = auto1_collider.Distance(wall_left.GetComponent<Collider2D>());
    244.         //Debug.Log("Dist_left" + collidist_left.distance);
    245.         collidist_above = auto1_collider.Distance(wall_above.GetComponent<Collider2D>());
    246.         //Debug.Log("Dist_above" + collidist_above.distance);
    247.  
    248.         min_dis_to_wall = Mathf.Min(Mathf.Min(collidist_right.distance, collidist_left.distance), collidist_above.distance);
    249.  
    250.  
    251.  
    252.     }
    253.  
    254.     void OnCollisionEnter2D(Collision2D collision)
    255.     {
    256.  
    257.         collied = true;
    258.     }
    259.  
    260.     void OnCollisionExit(Collision2D collision)
    261.     {
    262.         collied = false;
    263.     }
    264. }
    265.  
    266.  

    The error is :
    ArgumentOutOfRangeException: Argument is out of range.
    Parameter name: index

    I tried to find the error with debugging, but the only thing that I could find out is that the error lies in the update_swarm() function.

    Can someone help?
     
  2. Scabbage

    Scabbage

    Joined:
    Dec 11, 2014
    Posts:
    268
    It means you're trying to access an element out of the range of the array, like -1 or element 7 of a 5 element array.

    The error should also give you the line number where the exception was thrown. Whatever that line is, that's where the out of range index is.
     
  3. Xman236

    Xman236

    Joined:
    Mar 5, 2018
    Posts:
    11
    Thanks for the reply. The error occurs at the line 152. But the range of this array is 1, since particle_number =1. And I am accessing that line with update_swarm(num_of_particles); in line 221, which seems no problem at all...
     
  4. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Arrays are indexed from zero. So if you try to access element one of a one element array, it will be out of bounds - you would need to access array element zero in that case.

    Try stepping the code through in the debugger. That will prove very helpful and informative and you will be able to see exactly where the problem is.
     
    Xman236 likes this.