dimanche 12 janvier 2020

IndexOutOfRange when player reaches maxLevel

I'm having difficulties with this code, getting an IndexOutOfRange error everytime the player hits maxLevel. After hitting maxLevel, currentEXP will reset to 0, only after hitting 'K' again.

Could it be that I'm missing another if-statement. Maybe inside the while-loop? Encasing the while-loop? What condition would it have?

// Update is called once per frame
void Update()
{
    if (Input.GetKeyDown(KeyCode.K))
    {
        AddExp(100);
    }
}

//Adds EXP and levels up the character
public void AddExp(int expToAdd)
{
    currentEXP += expToAdd;

    //performs functions as long as playerLevel doesn't exceed maxLevel
    if (playerLevel < maxLevel)
    {
        //level up to the equivalent level with all the exp gain up to that level
        while (currentEXP > expToNextLevel[playerLevel])
        {
                currentEXP -= expToNextLevel[playerLevel];
                playerLevel++;
        }

    } else {
        currentEXP = 0;
    }
}

Aucun commentaire:

Enregistrer un commentaire