- Home /
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++;
}
}
Answer by madks13 · Aug 20, 2018 at 10:35 AM
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()
}
Thank you so much! This has it working right. And, I have to say the solution is so simple now that I see it lol
If so, can you accept this answer as the right one? Other people might have the same problem as yourself and will find this answer much easier with an accepted answer.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How do I put a delay in this? 2 Answers
How to rotate camera diagonally over players shoulder while still facing towards players direction 0 Answers
Unity 3D 2018.1 - Save 0 Answers