I am making a space shooter and in the bottom left corner I have my stat displayed in GUI. The onGUI is I think only called when the game starts because when I change variable in a script it should change the GUI to since it gets its numbers from that script!
private void OnGUI()
{
Vector2 targetPos = Vector2.zero;
////draw Armor
//VitalStat armor = GetStat(StatType.Armor) as VitalStat;
//VitalStat health = GetStat(StatType.Health) as VitalStat;
//VitalStat shield = GetStat(StatType.Shield) as VitalStat;
//VitalStat energy = GetStat(StatType.Energy) as VitalStat;
if (GetComponent<Ship>() != null)
{
targetPos = new Vector2(95, Screen.height - 65);
int row = 0;
if (Armor.CurrentValue > 0)
{
GUIUtil.DrawBar(targetPos, 150, 15, row, Armor.CurrentValue, Armor.Value, "Armor");
}
else
//draw Hp
{
GUIUtil.DrawBar(targetPos, 150, 15, row, Health.CurrentValue, Health.Value, "Health");
}
//draw Shield
if (Shield.Value > 0)
{
row++;
GUIUtil.DrawBar(targetPos, 150, 15, row, Shield.CurrentValue, Shield.Value, "Shield");
}
//draw energy
if (Energy.Value > 0)
{
row++;
GUIUtil.DrawBar(targetPos, 150, 15, row, Energy.CurrentValue, Energy.Value, "Energy");
}
}
else if (GetComponent<Enemy>() != null)// && _hitTimer > Time.time
{
targetPos = Camera.main.WorldToScreenPoint(transform.position);
int row = 0;
//draw Shield
if (Shield.CurrentValue > 0)
{
GUIUtil.DrawBar(targetPos, row, Shield.CurrentValue, Shield.Value, "Shield");
}
else if (Armor.CurrentValue > 0)
{
GUIUtil.DrawBar(targetPos, row, Armor.CurrentValue, Armor.Value, "Armor");
}
else
//draw Hp
{
GUIUtil.DrawBar(targetPos, row, Health.CurrentValue, Health.Value, "Health");
}
//draw energy
if (Energy.Value > 0)
{
row++;
GUIUtil.DrawBar(targetPos, row, Energy.CurrentValue, Energy.Value, "Energy");
}
}
}
This is where I draw my GUI.
As you can see it gets Armor.Currentvalue and when I change that number the GUI doesn't change does somebody know why or how I can fix it?
UPDATE
This is the stat that keeps track of the health.
public VitalStat Health = new VitalStat() { Type = VitalStatType.Health };
A VitalStat has this:
[HideInInspector]
public float CurrentValue;
[HideInInspector]
public bool ShouldRegenerate = true;
public VitalStatType Type;
public StatType RegenerationRate;
public StatType RegenerationStartDelay;
private Stat _rate;
private Stat _delay;
private float _regenDelayTimer = 0;
This is how the current value gets calculated:
if (ShouldRegenerate && Time.time > _regenDelayTimer && _rate != null)
{
CurrentValue = Mathf.Min(Value, CurrentValue + (_rate.Value * Time.deltaTime));
}
This is the value:
protected float _value;
public virtual float Value { get { return _value; } }
And the value gets sets by the basevalue:
public virtual void Init(StatManager statMngr)
{
statManager = statMngr;
_value = BaseValue;
CalculateStatValue();
}
And the basevalue is set here where I change my health input:
if (!string.IsNullOrEmpty(val))
{
try
{
float intVal = Convert.ToSingle(val);
target.BaseValue = intVal;
//Safe to Database!
}
catch (Exception e)
{
Debug.Log("Invalid value " + e.Message);
}
}
I hope this clarify's it a little bit