0
\$\begingroup\$

So, I have a list of Upgrade systems for my game but it has a bug on it when I press upgrade for a second time the index out of range then pressed for the third times the index out of range gone. this whole script.

void Start()
{
    newSelectedIndex = previousSelectedIndex = PlayerPrefs.GetInt("currentPlayer");
    btn = Select_Player[newSelectedIndex].GetComponent<Button>();
    btn.interactable = false;

    Coins = M_CoinManager.instance.Coins;
    for (int i = 0; i < Select_Player.Length; i++)
    {
        priceText[i].text = Cost_Player[i].ToString();
        value_player[i] = "" + i;
        Select_Player[i].SetActive(false);
        buyPlayer[i] = PlayerPrefs.GetInt(value_player[i]);
    }
    //if(PlayerPrefs.HasKey("currentPlayer")){
    selectedVehicleIndex = PlayerPrefs.GetInt("currentPlayer");
   
    theVehicles[selectedVehicleIndex].SetActive(true);
    VehicleInfo();
    UpgradeButtonStatus();
}

// Update is called once per frame
void Update()
{
    for (int i = 0; i < Select_Player.Length; i++)
    {
        buyPlayer[i] = PlayerPrefs.GetInt(value_player[i]);
        if (i == buyPlayer[i])
        {
            Select_Player[i].SetActive(true);
            upgradeBtn[i].interactable = true;
            UpgradeButtonStatus();

        }
        else
        {
            upgradeBtn[i].interactable = false;
        }
    }
}

public void BuyCharact(int id)
{
    M_SoundManager.instance.playUIsfx();
    if (Cost_Player[id] <= Coins)
    {
        M_CoinManager.instance.AddCoins(-Cost_Player[id]);
        PlayerPrefs.SetInt(value_player[id], id);

    }
    else
    {
        _CoinShake.DoShake();
        Debug.Log("Does have enough coin");
    }
}

public void Reset()
{
    for (int i = 0; i < Select_Player.Length; i++)
    {
        PlayerPrefs.SetInt(value_player[i], 0);
    }
}

public void Select(int id)
{
    previousSelectedIndex = newSelectedIndex;
    newSelectedIndex = id;
    newSelectedIndex = selectedVehicleIndex;

    PlayerPrefs.SetInt("currentPlayer", newSelectedIndex);
    Button newbtn = Select_Player[previousSelectedIndex].GetComponent<Button>();
    btn = Select_Player[newSelectedIndex].GetComponent<Button>();
    btn.interactable = false;
    newbtn.interactable = true;
    M_SoundManager.instance.playUIsfx();
    Debug.Log("Selected TypeCar" + newSelectedIndex);
}

public void nextVehicle()
{
    M_SoundManager.instance.playUIsfx();
    theVehicles[selectedVehicleIndex].SetActive(false);
    selectedVehicleIndex = (selectedVehicleIndex + 1) % theVehicles.Length;
    theVehicles[selectedVehicleIndex].SetActive(true);
    VehicleInfo();
    UpgradeButtonStatus();
}

public void PreviousVehicle()
{
    M_SoundManager.instance.playUIsfx();
    theVehicles[selectedVehicleIndex].SetActive(false);
    selectedVehicleIndex--;
    if (selectedVehicleIndex < 0)
    {
        selectedVehicleIndex += theVehicles.Length;
    }
    theVehicles[selectedVehicleIndex].SetActive(true);
    VehicleInfo();
    UpgradeButtonStatus();
}

public void VehicleInfo()
{
    currLevel = _VehicleStats[0].carItem[selectedVehicleIndex].unlockedLevel;
    levelText[selectedVehicleIndex].text = "Level: " + (currLevel + 1);
    powerText[selectedVehicleIndex].text = "Power: " + _VehicleStats[0].carItem[selectedVehicleIndex].vehicleLevel[currLevel].motorPower;
    brakeText[selectedVehicleIndex].text = "Brake: " + _VehicleStats[0].carItem[selectedVehicleIndex].vehicleLevel[currLevel].brakePower;

    
    Debug.Log(selectedVehicleIndex);
}

public void upgradeMethod()
{
    nextLevelIndex = _VehicleStats[0].carItem[selectedVehicleIndex].unlockedLevel + 1;
    if (Coins >= _VehicleStats[0].carItem[selectedVehicleIndex].vehicleLevel[nextLevelIndex].unlockCost)
    {
        Coins -= _VehicleStats[0].carItem[selectedVehicleIndex].vehicleLevel[nextLevelIndex].unlockCost;
        _VehicleStats[0].carItem[selectedVehicleIndex].unlockedLevel++;

        if (_VehicleStats[0].carItem[selectedVehicleIndex].unlockedLevel < _VehicleStats[0].carItem[selectedVehicleIndex].vehicleLevel.Length - 1)
        {
            upgradeBtnText[selectedVehicleIndex].text = "Upgrade Cost :" + _VehicleStats[0].carItem[selectedVehicleIndex].vehicleLevel[nextLevelIndex + 1].unlockCost;
        }
        else
        {
            upgradeBtn[selectedVehicleIndex].interactable = false;
            upgradeBtnText[selectedVehicleIndex].text = "Max Level";
        }
        VehicleInfo();
    }

}

private void UpgradeButtonStatus()
{
    if (_VehicleStats[0].carItem[selectedVehicleIndex].unlockedLevel < _VehicleStats[0].carItem[selectedVehicleIndex].vehicleLevel.Length - 1)
    {
        upgradeBtn[selectedVehicleIndex].interactable = true;
        nextLevelIndex = _VehicleStats[0].carItem[selectedVehicleIndex].unlockedLevel + 1;
        upgradeBtnText[selectedVehicleIndex].text = "Upgrade Cost :" + _VehicleStats[0].carItem[selectedVehicleIndex].vehicleLevel[nextLevelIndex + 1].unlockCost;
    }
    else
    {
        upgradeBtn[selectedVehicleIndex].interactable = false;
        upgradeBtnText[selectedVehicleIndex].text = "Max Level";
    }
}

and the error reference to this line :

private void UpgradeButtonStatus()
{
    if (_VehicleStats[0].carItem[selectedVehicleIndex].unlockedLevel < _VehicleStats[0].carItem[selectedVehicleIndex].vehicleLevel.Length - 1)
    {
        upgradeBtn[selectedVehicleIndex].interactable = true;
        nextLevelIndex = _VehicleStats[0].carItem[selectedVehicleIndex].unlockedLevel + 1;
        upgradeBtnText[selectedVehicleIndex].text = "Upgrade Cost :" + _VehicleStats[0].carItem[selectedVehicleIndex].vehicleLevel[nextLevelIndex + 1].unlockCost;
    }
    else
    {
        upgradeBtn[selectedVehicleIndex].interactable = false;
        upgradeBtnText[selectedVehicleIndex].text = "Max Level";
    }

}

as you can see on the update that's what I mean. after I press upgrade for the third times the error stopped.

and this from the inspector Inspector

\$\endgroup\$
1
  • 1
    \$\begingroup\$ You have a lot of indexing happening in this code. It would help if you pointed us to the one specific line where this error is thrown. \$\endgroup\$
    – DMGregory
    Nov 25 '20 at 13:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.