- Home /
The question is answered, right answer was accepted
Values lost?
Hi,
I am trying to optimize code re-usage but have run into the problem of any values set in the inspector are being read as zero. The code in question should be a base class with the most simple implementation of all common stats and methods and a sub class with properties and methods specific to an asteroid (that you wouldn't find on a fighter etc).
I put the sub class on a game object and the debug log reads -50 where it should read 50. removing the subtraction at the start of the sub class, it then reads 0. Any help would be great.
public class BaseStats : MonoBehaviour {
// ** settings ** //
// ** bar stats ** //
public float defaultHealth = 100f;
public float defaultRegen = 2.5f;
public float defaultArmour = 100f;
public float defaultRepair = 2.5f;
// ** quals ** //
public int qualification = 0;
// ** collected values ** //
protected StatValue health;
protected StatValue regen;
protected StatValue armour;
protected StatValue repair;
// Use this for initialization
void Start ()
{
// Create struct values for each stat that needs them. Created here because they collect values from the inspector.
health = new StatValue(defaultHealth,qualification);
regen = new StatValue(defaultRegen,qualification);
armour = new StatValue(defaultArmour,qualification);
repair = new StatValue(defaultRepair,qualification);
}
// methods follow
And the second class
using UnityEngine;
using System.Collections;
public class AsteroidStats : BaseStats {
// Use this for initialization
void Start ()
{
health.currentStat -= 50;
}
// Update is called once per frame
void Update ()
{
// replenish statbars
replenlenishStat(health,defaultHealth,regen.currentStat);
replenlenishStat(armour,defaultArmour,repair.currentStat);
Debug.Log(health.currentStat);
}
}
A stat value is a struct which creates current, bonus and experience values for each default stat as well as implementing common methods.
Answer by iwaldrop · Dec 31, 2014 at 08:23 PM
The Start method in your base class is not being called. You need to make it virtual and then override it from the inheriting class. Another thought would be to move your base class Start stuff to Awake.
Changing start to Awake() in the base class has done it! Thankyou very much!