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

Question IndexOutOfRangeException: Index was outside the bounds of the Array

Discussion in 'Scripting' started by Hannibal11, Jan 26, 2021.

  1. Hannibal11

    Hannibal11

    Joined:
    Mar 5, 2020
    Posts:
    15
    Hi at all,
    i am trying to spawn an array, but it gives me this error:
    "Index was outside the bounds of the array", and indicates the Function: "InstantiateFormation ()".
    I've seen a bit around, and tried putting ".Length" to the "axisX", and "axisZ" variables in the For cicles, but don't work, and it gives me another error.
    How can I do?
    Please ...
     

    Attached Files:

  2. mopthrow

    mopthrow

    Joined:
    May 8, 2020
    Posts:
    343
    Checking the obvious maybe, but is your formation array empty? Maybe you forgot to add the GameController tag to your objects.

    Deleted my previous answer as it was incorrect, sorry!
     
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    IndexOutOfRange is a common error. It means you're outside the size of your collection (in this case, your array). You should be adding debug statements to check for size and content to make sure you have the values you expect. My guess is your array is empty, so a random.range is going to return a value that isn't within your array's size. If your array is empty, you shouldn't try to instantiate from it. (Note, it's a good idea to add this check and handle it even when you do expect values to be in your array)
     
    Hannibal11 and mopthrow like this.
  4. Hannibal11

    Hannibal11

    Joined:
    Mar 5, 2020
    Posts:
    15
    Hi! thanks for your help,
    My Array, as you can see, is full of 4 capsules that will be arranged randomly: 8 on the X axis and 10 on the Z axis.
    If I fill the array with Capsules from the Hierarchy, Unity crashes (I think due to an infinite loop).
    If, on the other hand, I put the Capsule prefabs in the array it gives me this error.
    What can it be? I do not understand
     

    Attached Files:

  5. Hannibal11

    Hannibal11

    Joined:
    Mar 5, 2020
    Posts:
    15
    Brathnann, you are right, when I press play, suddenly the Array empties
     
  6. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    That means your call in Start is overwriting your array. You don't need that call if you are dragging and dropping in the inspector. As @mopthrow mentioned, if you have nothing in your scene with the tag GameController, the call finds nothing and you'll have an empty array.

    Since you are looping and using Instantiate to create random items, my guess is you are just trying to clone prefabs. Prefabs don't exist in the scene by default, which means calls to find them in the scene don't work, even if they have the proper tag on them.

    Comment out the formation = call and you should be fine since you added them by the inspector.
     
    Hannibal11 and Vryken like this.
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    Hannibal11 likes this.