Search Unity

Problem with Array of Custom Class and Index out of range

Discussion in 'Scripting' started by juliushg, Apr 24, 2015.

  1. juliushg

    juliushg

    Joined:
    Mar 27, 2015
    Posts:
    18
    I have this code that checks any objects with raycast in a grid type area, let's say 10 x 10. The objective is to get an array filled with all enemies in the area, like:

    *entitiesToGet[0].entName = "enemy one";

    entitiesToGet[0].entActualPos = Vector 3(1,0,5)*

    and so on.

    The code is this:


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4.  public class CheckArea : MonoBehaviour {
    5.  
    6.  
    7.      [System.Serializable]
    8.      public class EntitiesInArea {
    9.        public string entName;
    10.        public Vector3 entActualPos;
    11.      }
    12.  
    13.      public EntitiesInArea[] entitiesToGet;
    14.  
    15.      void Start() {
    16.  
    17.         entitiesToGet[0] = new EntitiesInArea();
    18.  
    19.      }
    20.  
    21.      void CheckArea () {
    22.  
    23.      
    24.          int entityNumber = 0;
    25.  
    26.        for (int z = 0; z < 10; z++) {
    27.      
    28.          for (int x = 0; x < 10; x++) {
    29.  
    30.            RaycastHit[] hits;
    31.            hits = Physics.RaycastAll(new Vector3(x, 2, z), Vector3.down, 1f);
    32.            int i = 0;
    33.            while (i < hits.Length) {
    34.              RaycastHit hit = hits[i];
    35.  
    36.              // this is to check which entity is in this cell (x and z)
    37.  
    38.              if (hit.collider.gameObject.tag == "Enemies") {
    39.  
    40.                entitiesToGet[entityNumber] = new EntitiesInArea();
    41.                entitiesToGet[entityNumber].entName =hit.collider.gameObject.name;
    42.                entitiesToGet[entityNumber].entActualPos = hit.transform.position;
    43.                entityNumber++;
    44.  
    45.              }
    46.  
    47.              //Debug.Log ("Who is here?:" + hit.collider.gameObject.name);
    48.  
    49.              i++;
    50.            }
    51.  
    52.          }
    53.  
    54.        }
    55.  
    56.  
    57.      }
    58.  
    59.    }

    At first I was getting the "NullReferenceException: Object reference not set to an instance of an object" error, but that was fixed with the "*[System.Serializable]*" line (after I did some research).

    Still, I'm sure something is wrong, because I keep getting the error *"IndexOutOfRangeException: Array index is out of range"* when trying to access the elements of the array. Obviously, when no objects with tag "Enemies" are found, the array must be empty and nothing is done. Please notice that the array will have a dynamic length, depending on how many enemies are found in the area. I think that's not the way to access elements of a class in an array.

    Please tell me what I'm doing wrong, thanks in advance.
     
  2. Ohrm

    Ohrm

    Joined:
    Dec 3, 2012
    Posts:
    4
    entitiesToGet[0] does not exist because you never initialize the array. Change "public EntitiesInArea[] entitiesToGet;" to "public EntitiesInArea[] entitiesToGet = new EntitiesInArea[SizeOfArray];"
     
  3. juliushg

    juliushg

    Joined:
    Mar 27, 2015
    Posts:
    18
    Thanks for the rapid response, I made the change on line 13 but I'm still getting the error. Please verify the lines 17 and 40, are they correct?
     
  4. juliushg

    juliushg

    Joined:
    Mar 27, 2015
    Posts:
    18
    Well, anyone? I've tried combinations of those lines but nothing works. A possible solution might be using a simple array for each field, but that's not elegant and not good for performance, I think.
     
  5. juliushg

    juliushg

    Joined:
    Mar 27, 2015
    Posts:
    18
    Ok, I got it now, it's working, I had to initialize with a number greater than 0, in "size of array", so the line "public EntitiesInArea[] entitiesToGet = new EntitiesInArea[5];" worked well.

    But, what if I don't know the number of entities in the area? There may be 5 or 10 or more enemies. The objective is that the array has to grow until the last object is listed. And that constrains me to a fixed length. How do I make it with dynamic size?
     
  6. juliushg

    juliushg

    Joined:
    Mar 27, 2015
    Posts:
    18
    I think my last related question has been missed, my bad.

    The question is: how can I establish the same array but with a dynamic size? Length of the array will depend on how many enemies are found in the area.
     
    Last edited: Apr 27, 2015
  7. juliushg

    juliushg

    Joined:
    Mar 27, 2015
    Posts:
    18
    I got it now, posting this for future references. I used a Generic List. That allows me to add or remove items at will.