Search Unity

IndexOutOfRangeException: Index was outside the bounds of the array.

Discussion in 'Scripting' started by JoshuaReidudt, Jul 22, 2019.

  1. JoshuaReidudt

    JoshuaReidudt

    Joined:
    Jun 26, 2019
    Posts:
    1
    The full error is

    "
    IndexOutOfRangeException: Index was outside the bounds of the array.
    GroundSpawnerScript.PositionThePlayer () (at Assets/scripts/GroundColliderScripts/GroundSpawnerScript.cs:249)
    GroundSpawnerScript.Start () (at Assets/scripts/GroundColliderScripts/GroundSpawnerScript.cs:46)

    "

    Honestly, I think I'm just being dumb, but I cant seem to find the fix to the error

    Here is the code:

    Code (CSharp):
    1.     void Start()
    2.     {
    3.  
    4.         controllX = 0;
    5.  
    6.         // the y position of the first ground is going to start from center
    7.         float center = Screen.width / Screen.height;
    8.  
    9.         center += 1.0f;
    10.  
    11.         CreateGround(center);
    12.  
    13.         for (int i = 0; i < collectables.Length; i++)
    14.         {
    15.             collectables[i].SetActive(false);
    16.         }
    17.  
    18.         // getting players script
    19.         playerScript = GameObject.Find("Player").GetComponentInParent<Player>();
    20.  
    21.         PositionThePlayer(); // this is line 46 which it seems to have an issue with.
    22.  
    23.  
    24.     }
    25.  




    Code (CSharp):
    1.     void PositionThePlayer()
    2.     {
    3.  
    4.         // getting back ground
    5.         GameObject[] deadlyGround = GameObject.FindGameObjectsWithTag("Deadly");
    6.  
    7.         // getting ground in game
    8.         GameObject[] groundInGame = GameObject.FindGameObjectsWithTag("Ground");
    9.  
    10.         for (int i = 0; i < deadlyGround.Length; i++)
    11.         {
    12.  
    13.             if (deadlyGround[i].transform.position.y == 0)
    14.             {
    15.  
    16.                 Vector3 t = deadlyGround[i].transform.position;
    17.  
    18.                 deadlyGround[i].transform.position = new Vector3(groundInGame[0].transform.position.x,
    19.                                                                groundInGame[0].transform.position.y,
    20.                                                                groundInGame[0].transform.position.z);
    21.  
    22.                 groundInGame[0].transform.position = t;
    23.  
    24.             }
    25.  
    26.         }
    27.  
    28.         Vector3 temp = groundInGame[0].transform.position; // here is line 249
    29.  
    30.         for (int i = 1; i < groundInGame.Length; i++)
    31.         {
    32.  
    33.             if (temp.y < groundInGame[i].transform.position.y)
    34.                 temp = groundInGame[i].transform.position;
    35.  
    36.         }
    37.  
    38.  
    39.         // positioning the player above the cloud
    40.         playerScript.transform.position = new Vector3(temp.x, temp.y + 0.8f, temp.z);
    41.  
    42.  
    43.     }
    44.  
    45.  
    46. }
    47.  
     
  2. WallaceT_MFM

    WallaceT_MFM

    Joined:
    Sep 25, 2017
    Posts:
    394
    My guess is that your CreateGround method either doesn't tag objects it creates, or it deactivates the game objects. FindObjectsWithTags only finds active game objects. So, line 249 tries to access an empty array. It is able to get there because the for loop before that doesn't do anything, since the other array is probably also empty, resulting in a length of 0.