Could not figure this one out but I get a NullReferenceException when I call the onLevelup event when I UpdateLevel().
If I comment out line 33 I get no errors. all this does is restore the players health and if I comment out the RestoreState() method onLevelup() still gives me a null reference exception. On further tests I found that when the next attack hits after the killing blow the code resolves and the health and level updates.
Thanks bixarrio!
Not sure why a null check would fix this (which it did)? If it was null then I can understand why this error was thrown but it is not because the code executed fine when I checked for null. Was the error caused by me not checking?
Strange I thought that this was adding the subscriber to the Health class when I put this in the Start()
The only thing I added to the code was the check and now it works?
If something calls BaseStats.GetStat() before any methods that subscribe to the event run their Start() (say if Health subscribes to onLevelUp, but another component gets a Stat in their Start() which happens to run before Health()) then the event will be empty. Even when you’re positive that the event will have subscribed listeners, you should null check.