NullReferenceException: Object reference not set to an instance of an object
RPG.Stats.Progression.GetLevels (RPG.Stats.Stat stat, RPG.Stats.CharacterClass characterClass) (at Assets/Scripts/Stats/Progression.cs:30)
RPG.Stats.BaseStats.GetLevel () (at Assets/Scripts/Stats/BaseStats.cs:31)
RPG.Stats.BaseStats.GetStat (RPG.Stats.Stat stat) (at Assets/Scripts/Stats/BaseStats.cs:22)
RPG.Attributes.Health.Start () (at Assets/Scripts/Attributes/Health.cs:17)
Not sure what the cause is. It’s happening on the Player gameObject, but if I make another scene with no enemies, more errors pop up. I think it has something to do with the progression dictionary not fully loading up by the time Awake() is called on the Player’s Health script.
First things first, we’ll need to see where the null reference is occuring.
Let’s start with your Progression.cs class, as that’s where the null reference is… Be sure to mark line 30 with a comment and we can go from there.
Ok, so it looks like BaseStats.GetLevel() is calling Progression.GetLevels(), but the lookup has not been initialized. As it’s a public method entry into Progression, it should also have a BuildLookup(); call in it to guarantee that the lookup has been created when it’s called.
public int GetLevels(Stat stat, CharacterClass characterClass)
{
BuildLookup(); //Add this line
float[] levels = lookUpTable[characterClass][stat];
return levels.Length;
}