- Home /
How to display Damage & Armor Stats in UI
Hi!
I want to display my armor and damage in my UI, however, I can't seem to wrap my head around how I should go about this. I managed to display my health by using this line:
inventoryHealth.text = currentHealth.ToString() + "/" + maxHealth.ToString();
However, I can't seem to do this with my damage and armor because they are Stat values.
Here's how I set up my health armor and damage:
public int maxHealth = 100; public int currentHealth { get; protected set; }
public Stat damage;
public Stat armor;
the damage and armor are derived from another script which is the following:
using System.Collections; using System.Collections.Generic; using UnityEngine;
[System.Serializable] public class Stat {
public int baseValue;
private List<int> modifiers = new List<int>();
public int GetValue()
{
int finalValue = baseValue;
modifiers.ForEach(x => finalValue += x);
return finalValue;
}
public void AddModifier (int modifier)
{
if (modifier != 0)
modifiers.Add(modifier);
}
public void RemoveModifier (int modifier)
{
if (modifier != 0)
modifiers.Remove(modifier);
}
}
I hope anyone can help me.
Answer by WoppelPoppel · Apr 29, 2020 at 08:23 PM
The problem with using the ".ToString()" function on a "Stat" object is that it does not know what to return. Should it return a string of "baseValue"? Or the "modifiers"?
I would choose one of these two options:
The lazy approach:
inventoryArmor.text = armor.GetValue().ToString()
Doing it properly
Add a "ToString" function to the "Stat"-class
public String ToString() { return GetValue().ToString() }
This lets you do:
inventoryArmor.text = armor.ToString()
(you may need to use the "override" keyword, ie "public override String ToString()")