Problem with my level value not updating or displaying correctly

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.

I’m not 100% sure what’s happening, but this code is very different from mine. However, I can’t remember if the code I have includes my own changes.

float xpToLevelUp = progression.GetStat(Stat.ExperienceToLevelUp, characterClass, level);

I have this as a cumulative value. That is, with every iteration of the loop, the xpToLevelUp is increased by the level’s requried amount.

Also

if (xpToLevelUp < currentXP)
{
    return level;
}

I have this the other way around

if (currentXP < xpToLevelUp)
{
    return level;
}

I don’t know how many levels you defined but is it possible that it actually returns 5 because it equals penultimateLevel + 1?

I have the level and experience to level up set the same as the lessons with 4 options then the final level as level 5 if the amount of exp reaches past the total needed to go past level 4. But with experience set at zero, then the level should be tested and set to level 1 at the start.

But your code doesn’t do that. It calculates the level all the time. So, it sets it to 5. I suspect if you change the text in the editor to something other than 5, it will still be 5 when the game runs

You are correct.

I think if you change this to

            if (currentXP < xpToLevelUp)
            {
                return level;
            }

it will work

It worked!

Great stuff. Enjoy the rest of the course.

Remember to mark the topic as solved

Good job spotting that, @bixarrio!!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms