OnLevelUp event for the effect, too

Now that we have added an event to subscribe to, instead of just adding a line in UpdateLevel() to fire it, I also changed BaseStats so that LevelEffect() gets subscribed to the event. Where UpdateLevel() previously “knew” about the effect, from now on it doesn’t anymore.

    public class BaseStats : MonoBehaviour
    {
        public event Action OnLevelUp;

        private void Start()
        {
            currentLevel = CalculateLevel();
            experience = GetComponent<Experience>();
            if (null != experience)
            {
                experience.OnExperienceGained += UpdateLevel;
            }
            OnLevelUp += LevelUpEffect;
        }

        private void OnDestroy()
        {
            if (null != experience)
            {
                experience.OnExperienceGained -= UpdateLevel;
            }
            OnLevelUp -= LevelUpEffect;
        }

        private void UpdateLevel()
        {
            int newLevel = CalculateLevel();
            if (newLevel > currentLevel)
            {
                currentLevel = newLevel;
                // Debug.Log($"{name} levelled up to {currentLevel}!");
                OnLevelUp();
            }
        }

        private void LevelUpEffect()
        {
            if (null == levelUpParticleEffect) return;
            Instantiate(levelUpParticleEffect, transform);
        }
    }

Since I subscribe to OnLevelUp from within the same script, there is actually no need to null check on it in UpdateLevel().

Privacy & Terms