I have completed the code and lecture for this lesson but for some reason my level value is not updating correctly.
These images are what my setup looks like. I have the hud design setup as followed along but then in gave the level stays at the number 5. not going to 1 instead. I checked and the code for the LevelDisplay.cs script is correct.
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
namespace RPG.Stats
{
public class LevelDisplay : MonoBehaviour
{
BaseStats baseStats;
private void Awake()
{
baseStats = GameObject.FindWithTag("Player").GetComponent<BaseStats>();
}
// Start is called before the first frame update
// void Start(){}
// Update is called once per frame
void Update()
{
GetComponent<Text>().text = String.Format("{0:0}", baseStats.GetLevel());
}
}
}
I checked and walked through the code from Experience.cs and BaseStats.cs everything is correct that I can see unless I am missing something.
Experience.cs Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using RPG.Saving;
using System;
namespace RPG.Stats
{
public class Experience : MonoBehaviour, ISaveable
{
[SerializeField] float experiencePoints = 0f;
public event Action onExperienceGained;
// Start is called before the first frame update
// void Start(){}
// Update is called once per frame
// void Update(){}
public void GainExperience(float experience)
{
experiencePoints += experience;
onExperienceGained();
}
public float GetPoints()
{
return experiencePoints;
}
public object CaptureState()
{
return experiencePoints;
}
public void RestoreState(object state)
{
experiencePoints = (float) state;
}
}
}
BaseStats.cs Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace RPG.Stats
{
public class BaseStats : MonoBehaviour
{
[Range(1, 99)]
[SerializeField] int startingLevel = 1;
[SerializeField] CharacterClass characterClass;
[SerializeField] Progression progression = null;
int currentLevel = 0;
// Start is called before the first frame update
void Start()
{
currentLevel = CalculateLevel();
Experience experience = GetComponent<Experience>();
if (experience != null)
{
experience.onExperienceGained += UpdateLevel;
}
}
// Update is called once per frame
// void Update(){}
void UpdateLevel()
{
int newLevel = CalculateLevel();
if (newLevel > currentLevel)
{
currentLevel = newLevel;
print("Leveled Up!");
}
}
public float GetStat(Stat stat)
{
return progression.GetStat(stat, characterClass, GetLevel());
}
public int GetLevel()
{
if (currentLevel < 1)
{
currentLevel = CalculateLevel();
}
return currentLevel;
}
public int CalculateLevel()
{
Experience experience = GetComponent<Experience>();
if (experience == null)
{
return startingLevel;
}
float currentXP = experience.GetPoints();
int penultimateLevel = progression.GetLevels(Stat.ExperienceToLevelUp, characterClass);
for (int level = 1; level <= penultimateLevel; level++)
{
float xpToLevelUp = progression.GetStat(Stat.ExperienceToLevelUp, characterClass, level);
if (xpToLevelUp < currentXP)
{
return level;
}
}
return penultimateLevel + 1;
}
}
}
I am not sure what I missed. I know it has to be something simple that I am overlooking but I am not sure. I was at first getting the out of bounds array error that was mentioned in the lesson and i fixed that and the error no longer occurs but the level does not need to update and change as needed.