Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at id.unity.com during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.


    Read our full announcement for more information and let us know if you have any questions.

Index was outside the bounds of the array

Discussion in 'Getting Started' started by eternatelove, Sep 13, 2023.

  1. eternatelove

    eternatelove

    Joined:
    Jun 25, 2021
    Posts:
    5
    IndexOutOfRangeException: Index was outside the bounds of the array.
    CarSelection.UpdateUI () (at Assets/Scripts/CarSelection.cs:54)
    CarSelection.SelectCar (System.Int32 _index) (at Assets/Scripts/CarSelection.cs:50)
    CarSelection.Start () (at Assets/Scripts/CarSelection.cs:29)

    The Code:

    using UnityEngine;
    using UnityEngine.PlayerLoop;
    using UnityEngine.UI;

    public class CarSelection : MonoBehaviour
    {
    [Header ("Navigation Buttons")]
    [SerializeField] private Button previousButton;
    [SerializeField] private Button nextButton;

    [Header("Play/Buy Buttons")]
    [SerializeField] private Button play;
    [SerializeField] private Button buy;
    [SerializeField] private Text priceText;

    [Header("Car Attributes")]
    [SerializeField] private int[] carPrices;
    private int currentCar;
    private GameObject[] carList;

    //[Header("Sound")]
    //[SerializeField] private AudioClip purchase;
    //private AudioSource source;

    private void Start()
    {
    //source = GetComponent<AudioSource>();
    currentCar = SaveManager.instance.currentCar;
    SelectCar(currentCar);

    currentCar = PlayerPrefs.GetInt("CarSelected");

    carList = new GameObject[transform.childCount];

    for(int i = 0; i < transform.childCount; i++)
    carList = transform.GetChild(i).gameObject;

    foreach(GameObject go in carList)
    go.SetActive(false);

    if(carList[currentCar])
    carList[currentCar].SetActive(true);
    }

    private void SelectCar(int _index)
    {
    for (int i = 0; i < transform.childCount; i++)
    transform.GetChild(i).gameObject.SetActive(i == _index);

    UpdateUI();
    }
    private void UpdateUI()
    {
    if (SaveManager.instance.carsUnlocked[currentCar])
    {
    play.gameObject.SetActive(true);
    buy.gameObject.SetActive(false);
    }
    else
    {
    play.gameObject.SetActive(false);
    buy.gameObject.SetActive(true);
    priceText.text = carPrices[currentCar] + "$";
    }
    }

    private void Update()
    {
    if (buy.gameObject.activeInHierarchy)
    buy.interactable = (SaveManager.instance.money >= carPrices[currentCar]);
    }

    public void ChangeCar(int _change)
    {
    currentCar += _change;

    if (currentCar > transform.childCount - 1)
    currentCar = 0;
    else if (currentCar < 0)
    currentCar = transform.childCount - 1;

    SaveManager.instance.currentCar = currentCar;
    SaveManager.instance.Save();
    SelectCar(currentCar);
    }
    public void BuyCar()
    {
    SaveManager.instance.money -= carPrices[currentCar];
    SaveManager.instance.carsUnlocked[currentCar] = true;
    SaveManager.instance.Save();
    //source.PlayOneShot(purchase);
    UpdateUI();
    }
    }
     
  2. tleylan

    tleylan

    Joined:
    Jun 17, 2020
    Posts:
    698
    We don't see any line numbers on the code but if you check your code and maybe log the index value you assume is correct you should see that the index is outside the bounds.
     
  3. eternatelove

    eternatelove

    Joined:
    Jun 25, 2021
    Posts:
    5
    It's in the UpdateUI() function.
     
  4. tleylan

    tleylan

    Joined:
    Jun 17, 2020
    Posts:
    698
    Well how many array accesses do you have there? What is the index value? It sounds like it is outside the bounds.
     
  5. eternatelove

    eternatelove

    Joined:
    Jun 25, 2021
    Posts:
    5
    There is a single array in if statement that is carsUnlocked[] and one in else statement that is carPrices[].
     
  6. tleylan

    tleylan

    Joined:
    Jun 17, 2020
    Posts:
    698
    I meant how many arrays that it would be hard to check. They use currentCar as an index doesn't it sound like that might be the value that is out of bounds? Check the value.
     
  7. ijmmai

    ijmmai

    Joined:
    Jun 9, 2023
    Posts:
    188
    Add code tags to your initial post. Once you save the edit, you will understand why people ask you to do this.