Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Index out of bounds? Despite having an array with that index?

Discussion in 'Scripting' started by TyrantKoala, Mar 31, 2019.

  1. TyrantKoala

    TyrantKoala

    Joined:
    Jul 24, 2017
    Posts:
    9
    I have an array of GameObjects called characterList.

    I want to set a playerpref to the name of an object in this gameobject array but it continuously gives me an error on this line

    characterName = CharacterList[playerId].name;

    I have set the playerId parameter when the button is clicked to 0


    Have no clue about why this is happening...


    Code below:






    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5. public class CharacterChoose : MonoBehaviour
    6. {
    7.     public GameObject[] characterList;
    8.     private int index;
    9.     public string characterName;
    10.  
    11.  
    12.  
    13.     // Use this for initialization
    14.     private void Start()
    15.     {
    16.  
    17.         index = PlayerPrefs.GetInt("CharacterSelected");
    18.         characterList = new GameObject[transform.childCount];
    19.        
    20.        //fill with models
    21.         for (int i = 0; i < transform.childCount; i++)
    22.         {
    23.             characterList[i] = transform.GetChild(i).gameObject;
    24.            
    25.         }
    26.         //toggle them off
    27.         /* foreach (GameObject go in characterList)
    28.              go.SetActive(false);
    29.          */
    30.  
    31.  
    32.  
    33.     }
    34.  
    35.  
    36.     public void onClick(int playerId)
    37.     {
    38.         index = playerId;
    39.  
    40.         PlayerPrefs.SetInt("CharacterSelected", index);
    41.          characterName = characterList[index].name;
    42.  
    43.       PlayerPrefs.SetString("CharacterName", characterName);
    44.  
    45.  
    46.         SceneManager.LoadScene("Worlds");
    47.     }
    48.  
    49.  
    50. }
     
  2. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    If
    playerId
    is zero and you are still getting an out of bounds error, that implies there are no items in the list. Indices start counting from zero.

    Have you checked whether line 23 is adding items - i.e. is
    transform.childCount
    on line 18 returning zero (meaning there is no element with index [0])?
     
  3. TyrantKoala

    TyrantKoala

    Joined:
    Jul 24, 2017
    Posts:
    9
    When I make it a public Array it shows on the side bar during the runtime that indeed an array full of game objetcs is being created
     
  4. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Ok. So in
    onClick()
    , have you tried logging out the values of
    playerId
    and
    characterList.Count
    ?
     
  5. TyrantKoala

    TyrantKoala

    Joined:
    Jul 24, 2017
    Posts:
    9
    So i put some debug logs, and I put one at the end of the code in side the start method and it printed out 13, however when i put it inside the click method it said 0
     
  6. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    First, make all your member variables private. That way, they can only be updated from within that script. Next, is to find out all the places in the script that write to
    characterList
    . One of them must be clearing it.
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    Is it possible you inadvertently added this script somewhere else in your scene or on some other prefab?

    When the game is running and you're seeing the error, press pause (not stop!) and in the Hierarchy window do a search for your script.

    For your case you can do this by searching t:CharacterChoose in the search field on the Hierarchy window.
     
    Last edited: Mar 31, 2019
    dgeimz and Doug_B like this.
  8. dgeimz

    dgeimz

    Joined:
    Jan 27, 2021
    Posts:
    1
    This is an old post, but I'm incredibly grateful for it. I've been so confused why all of my rotated 3d floor tiles will instantiate except one prefab in the array. Turns out I have the same script on that prefab with an empty array. You rock!
     
    Kurt-Dekker likes this.
  9. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    Awesome... glad to hear it! And here is one more generic blurb on index errors to save everybody time in the future:

    Some notes on IndexOutOfRangeException and ArgumentOutOfRangeException:

    http://plbm.com/?p=236