ExpManagement problems

Now, I have some code that I’m trying to write, and I can get the script to print the messages (so I can debug) and everything looks right in the inspector.


My issue, however, is that when I click to test the levelUp function each level seems to think that the XP required for a level up is the same as the amount of XP needed for a level 1 character. Here’s the code so you more knowledgeable minds out there can tell me what I’m not doing correctly and how best to correct any mistakes I’ve made:


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LevelUp : MonoBehaviour {

    public int currentLevel;
    public int currentXP;
    public int nextLevel;
    public int nextLevelXP;

    void Start() {
        currentLevel = 1;
        nextLevel = currentLevel++;
        nextLevelXP = currentLevel * nextLevel * 500;
    }

    void Update() {
        AddXP();
        if (currentXP < nextLevelXP) {
            print("You need " + (nextLevelXP - currentXP) + " to level up.");
        }
    }

    void AddXP() {
        if (Input.GetButtonUp("Fire1")) {
            currentXP += nextLevelXP;
        }
        if (currentXP >= nextLevelXP) {
            levelUp();
        }
    }

    void levelUp() {
        currentLevel++;
    }
}

This is easy :

  • You set up the nexLevelXp only at the start.
  • You need to update it each time you level up.

I’d suggest you change your code like this :

private void UpdateXP()
{
      nextLevelXP = currentLevel * nextLevel * 500;
}

private void LevelUp()
{
      currentLevel++;
      nextLevel = currentLevel + 1;
      UpdateXP();
}

void Start()
{
   //This will change currentLevel from 0 to 1
  // And update the rest
   LevelUp()
}