Need help with a levelUp system...

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;

    //Set the level, attributes, skills, and feats of the player
    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++;
    }
}

Hi @cvinson834

You’d better read the code yourself, I mean, line by line (explaining it to yourself).

There are problems like:

You never up the next level xp barrier, after you levelUp().

AddXP is strangely structured .You should maybe just query key press, then call method instead, not the otherway.

Also, in AddXP, if you always add the next level target amount with one click, what happens?
Shouldn’t you add some small step instead? Like addXP = 100… which you add every click, until you reach the next level xp barrier.

Also, you never update the nextLevel variable, when you go up in levels, it will show 1 all the time.