Index was outside the bounds of the array

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();
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();
}
}

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.

It’s in the UpdateUI() function.

Well how many array accesses do you have there? What is the index value? It sounds like it is outside the bounds.

There is a single array in if statement that is carsUnlocked[ ] and one in else statement that is carPrices[ ].

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.

Add code tags to your initial post. Once you save the edit, you will understand why people ask you to do this.