A
A
Alexander Abaishvili2021-08-24 10:41:39
C++ / C#
Alexander Abaishvili, 2021-08-24 10:41:39

How to fix error: index is out of bounds in array?

Good day to all! I ran into a problem: I made a skins store in the unit according to the guide on YouTube: https://www.youtube.com/watch?v=JbAsnrBRFBg&list=R... . At first, everything was fine (I checked with three skins), but later, when I added one more skin, an error appeared: index goes beyond the array. Who can tell me what to do, thanks!
Line error: info[index].isChosen = true;
Here is the code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using UnityEngine.UI;

public class SkinChanger : MonoBehaviour
{
    public Skin[] info;
    private bool[] StockCheck;

    public Button buyBttn;
    public TextMeshProUGUI priceText;
    public TextMeshProUGUI coinsText;
    public Transform player;
    public int index;

    public int coins;

    private void Awake()
    {
        coins = PlayerPrefs.GetInt("coins");
        index = PlayerPrefs.GetInt("chosenSkin");
        coinsText.text = coins.ToString();

        StockCheck = new bool[3];
        if (PlayerPrefs.HasKey("StockArray"))
            StockCheck = PlayerPrefsX.GetBoolArray("StockArray");

        else
            StockCheck[0] = true;

        info[index].isChosen = true;

        for (int i = 0; i < info.Length; i++)
        {
            info[i].inStock = StockCheck[i];
            if (i == index)
                player.GetChild(i).gameObject.SetActive(true);
            else
                player.GetChild(i).gameObject.SetActive(false);
        }

        priceText.text = "CHOSEN";
        buyBttn.interactable = false;
    }

    public void Save()
    {
        PlayerPrefsX.SetBoolArray("StockArray",StockCheck);
    }

    public void ScrollRight()
    {
        if (index < player.childCount)
        {
            index++;

            if (info[index].inStock && info[index].isChosen)
            {
                priceText.text = "CHOSEN";
                buyBttn.interactable = false;
            }
            else if (!info[index].inStock)
            {
                priceText.text = info[index].cost.ToString();
                buyBttn.interactable = true;
            }
            else if (info[index].inStock && !info[index].isChosen)
            {
                priceText.text = "CHOOSE";
                buyBttn.interactable = true;
            }

            for (int i = 0; i < player.childCount; i++)
                player.GetChild(i).gameObject.SetActive(false);
            // Можно записать так: player.GetChild(index-1).gameObject.SetActive(false);

            player.GetChild(index).gameObject.SetActive(true);
        }
    }

    public void ScrollLeft()
    {
        if (index > 0)
        {
            index--;

            if (info[index].inStock && info[index].isChosen)
            {
                priceText.text = "CHOSEN";
                buyBttn.interactable = false;
            }
            else if (!info[index].inStock)
            {
                priceText.text = info[index].cost.ToString();
                buyBttn.interactable = true;
            }
            else if (info[index].inStock && !info[index].isChosen)
            {
                priceText.text = "CHOOSE";
                buyBttn.interactable = true;
            }

            for (int i = 0; i < player.childCount; i++)
                player.GetChild(i).gameObject.SetActive(false);

            player.GetChild(index).gameObject.SetActive(true);
        }
    }

    public void BuyButtonAction()
    {
        if (buyBttn.interactable && !info[index].inStock)
        {
            if (coins > int.Parse(priceText.text))
            {
                coins -= int.Parse(priceText.text);
                coinsText.text = coins.ToString();
                PlayerPrefs.SetInt("coins", coins);
                StockCheck[index] = true;
                info[index].inStock = true;
                priceText.text = "CHOOSE";
                Save();
            }
        } 

        if (buyBttn.interactable && !info[index].isChosen && info[index].inStock) 
        {
            PlayerPrefs.SetInt("chosenSkin", index);
            buyBttn.interactable = false;
            priceText.text = "CHOSEN";
        }
    }
}


[System.Serializable]
public class Skin
{
    public int cost;
    public bool inStock;
    public bool isChosen;
}

When I added the skin, I changed StockCheck = new bool[3] to StockCheck = new bool[4]

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
pashara, 2021-08-24
@pashara

A non-existent index can be set in a line . The error itself says that an element whose index does not exist is selected (it can be either a negative number or a number greater than the array length - 1).
index = PlayerPrefs.GetInt("chosenSkin");

When I added the skin, I changed StockCheck = new bool[3] to StockCheck = new bool[4]

Well, here's why. It could be
StockCheck = new bool[info.Length]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question