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++;
}
}
Answer by eses · Aug 20, 2018 at 12:58 PM
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.
That's very good advice. I'm still learning, so sometimes I misunderstand even what I'm trying to say lol
AddXP was only there to test the amount of XP necessary to receive the next levelUp, because I'm borrowing the XP amounts from a very old game I loved when I was younger.
Aaaaaand realizing this, my code is working as I intended it to. haha Thank you so much for the help!
If you can, please accept the answer as correct one, if it worked.
Just did! Had input from someone else and they told me the exact same thing. Again, thank you so much for the advice! It's working exactly as it should!
Your answer
Follow this Question
Related Questions
how to add rigidbody to my mouseposition 1 Answer
What this error? 0 Answers
Best way of moving player 2 Answers
Remember position for a simple player controller (Left/Right)? 0 Answers
load scene after video ended 2 Answers