I am trying to create a condensed list of resistance/weaknesses for my health manager for my game, and am having some trouble with getting it working. My current approach is to create an array of constructors containing a string variable and a float variable, but I can’t quite get it to work. How it’s supposed to work is if the damage type string from the attack matches any of the weaknesses, then it applies a multiplier to the damage, and matching resistances apply reductions. Here’s what I have right now:
public class HealthManager : MonoBehaviour
{
[System.Serializable]
public class Weakness
{
public readonly string weakness;
public readonly float multiplier;
public Weakness (string weakness, float multiplier)
{
this.weakness = weakness;
this.multiplier = multiplier;
}
}
[System.Serializable]
public class Resistance
{
public readonly string resistance;
public readonly float reduction;
public Resistance(string resistance, float reduction)
{
this.resistance = resistance;
this.reduction = reduction;
}
}
public float health;
[SerializeField] private Weakness[] weaknesses;
[SerializeField] private Resistance[] resistances;
public void TakeDamage(float damage, string damageType)
{
foreach (var weakness in weaknesses)
{
if(damageType == weakness.weakness)
{
damage = damage * weakness.multiplier;
}
}
foreach ( var resistance in resistances)
{
if(damageType == resistance.resistance)
{
damage = damage * resistance.reduction;
}
}
health = health - damage;
}
}
Here’s how it’s appearing in the editor:
And here’s how I am trying to make it look:
If anyone has a tip or something to help out, let me know! Thank you.