- Home /
Why isn't playerLevel going up when I have enough xp?
public class HealthAndLevelSystems : MonoBehaviour {
public int experiencePoints;
public int playerLevel;
public float currentHealth;
public float maxHealth;
public GameObject player;
// Start is called before the first frame update
void Start()
{
experiencePoints = PlayerPrefs.GetInt("experiencePoints");
maxHealth = 10 + playerLevel;
currentHealth = maxHealth;
}
// Update is called once per frame
void Update()
{
PlayerPrefs.SetInt("experiencePoints", experiencePoints);
experiencePoints += 1;
if (currentHealth <= 0)
{
Die();
}
}
Dictionary<int, int> Levels = new Dictionary<int, int>()
{
{ 50, 1 },
{ 100, 2 },
{ 150, 3 },
{ 200, 4 },
};
void SetLevel(int ExperiencePoints)
{
foreach (KeyValuePair<int, int> pair in Levels)
{
if (pair.Key == ExperiencePoints)
{
playerLevel = pair.Value;
}
}
}
private void Die()
{
Time.timeScale = 0f;
Destroy(player);
}
}
Your SetLevel method looks solid. However, I don't see any place where you call the method. I suggest you add a call to SetLevel after every place where you change experiencePoints. For example, you could add SetLevel(experiencePoints) to line 25.
Answer by jackmw94 · Jan 15, 2021 at 10:22 AM
I think this code could be a fair bit easier to use if you have a single point at which you change your experience - this function could then update your player prefs and level:
private const string XPSaveKey = "experiencePoints"; // add this line at the top of your class
public void UpdateXP(int addXp, bool save = true)
{
experiencePoints += addXp;
SetLevel ( experiencePoints );
maxHealth = 10 + playerLevel;
if ( save )
{
PlayerPrefs.SetInt ( XPSaveKey );
}
}
You can then use this function whenever you want to update the player's xp. Save will usually be true during the game except for on start. When the game starts this is the only point at which the save data will be more relevant than the game data so your start function can load the xp then call this with save as false.
For your specific issue, I think TimBur was right - you're not calling SetLevel, thus that code to increment the level is never executing. Still felt this answer was relevant since your next issue might have been that incrementing the level doesn't adjust the max health!
Yup. Good points. This is a more elegant way to handle the exp/levelling process.
Answer by AbandonedCrypt · Jan 15, 2021 at 01:34 PM
Its even easier and also better to have the xp needed to level up in a separate variable.
public class Level
{
private int currentXp;
private int levelUpXp;
private int currentLevel;
public void AddXp(int xp)
{
currentXp += xp;
if(currentXp >= levelUpXp)
LevelUp();
}
private void LevelUp()
{
currentLevel++;
levelUpXP = GetNextLevelXP(currentLevel);
}
private void GetNextLevelXP(int level)
{
//look up the xp requirement for next level
}
}
This is by far not handling any special cases and should just demonstrate the principle. This has the added benefit of making the xp needed to level up accessible easily, so that they can be displayed next to the current XP in the character sheet.
Your answer
Follow this Question
Related Questions
New scene for minigame or not? 1 Answer
Display Audio Analysis and levels with objects 0 Answers
when i click the GUI button it doesnt load the level 1 Answer
Unity Serializer Wont Load Saves 1 Answer
Load previous scene 1 Answer