Hi Everyone,
After this lecture, I found that the enemies looked alive, but they were actually dead (and were one-hit, obviously). I couldn’t figure out why this is the case, because I double-checked all the scripts involved (Health, BaseStats, Progression) but I couldn’t find anything.
My Health Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using RPG.Saving;
using RPG.Stats;
using RPG.Core;
namespace RPG.Attributes
{
public class Health : MonoBehaviour, ISaveable
{
[SerializeField] float healthPoints = 100f;
bool isDead = false;
private void Awake()
{
healthPoints = GetComponent<BaseStats>().GetStat(Stat.Health);
}
public bool IsDead()
{
return isDead;
}
public void TakeDamage(GameObject instigator, float damage)
{
healthPoints = Mathf.Max(healthPoints - damage, 0);
if(healthPoints == 0)
{
Die();
AwardExperience(instigator);
}
}
public float GetPercentage()
{
return 100 * (healthPoints/ GetComponent<BaseStats>().GetStat(Stat.Health));
}
private void Die()
{
if (isDead) return;
isDead = true;
GetComponent<Animator>().SetTrigger("die");
GetComponent<ActionScheduler>().CancelCurrentAction();
}
private void AwardExperience(GameObject instigator)
{
Experience experience = instigator.GetComponent<Experience>();
if (experience == null) return;
experience.GainExperience(GetComponent<BaseStats>().GetStat(Stat.ExperienceReward));
}
public object CaptureState()
{
return healthPoints;
}
public void RestoreState(object state)
{
healthPoints = (float)state;
if(healthPoints == 0)
{
Die();
}
}
}
}
My BaseStats Script:
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;
public float GetStat(Stat stat)
{
return progression.GetStat(stat, characterClass, startingLevel);
}
}
}
Finally, my Progression.cs:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace RPG.Stats
{
[CreateAssetMenu(fileName = "Progression", menuName = "Stats/New Progression", order = 0)]
public class Progression : ScriptableObject
{
[SerializeField] ProgressionCharacterClass[] characterClasses = null;
public float GetStat(Stat stat, CharacterClass characterClass, int level)
{
foreach (ProgressionCharacterClass progressionClass in characterClasses)
{
if (progressionClass.characterClass != characterClass) continue;
foreach (ProgressionStat progressionStat in progressionClass.stats)
{
if (progressionStat.stat != stat) continue;
if (progressionStat.levels.Length <= level) continue;
return progressionStat.levels[level - 1];
}
}
return 0;
}
[System.Serializable]
class ProgressionCharacterClass
{
public CharacterClass characterClass;
public ProgressionStat[] stats;
}
[System.Serializable]
class ProgressionStat
{
public Stat stat;
public float[] levels;
}
}
}
By the way, here’s how it’s set up in the inspector if anyone needs it:
Thanks for the help!