Using Getters Question

I’m trying to use extensions for the first time in unity and I’m a bit confused on the implementation.

I have

public class Stats : MonoBehaviour{

    public int intelligence;
    public int agility;
    public int strength;
    public int health;
    public int damage;
    public int speed;

    public Stats()
    {
        intelligence = 0;
        agility = 0;
        strength = 0;
        health = 0;
        damage = 0;
        speed = 0;
    }

    public int getIntelligence()
    {
        return intelligence;
    }
    public int getAgility()
    {
        return agility;
    }
    public int getStrength()
    {
        return strength;
    }
    public int getHealth()
    {
        return health;
    }
    public int getDamage()
    {
        return damage;
    }
    public int getSpeed()
    {
        return speed;
    }

and in another script I have

public class Creature : Stats {

    void Start () {
        intelligence = getIntelligence();
        strength = getStrength();
        agility = getAgility();
        health = getHealth();
        damage = getDamage();
        speed = getSpeed();
	}

I have set the “Stats” in the inspector to all be 1 so I can see them change from 0 to 1 however this doesn’t work. Is this now how you do this? I feel like I’m not understanding the proper way to implement this. Could someone explain?

Thank you!

You are using constructor in a Monobehaviour derivative script. You are not supposed to do it as that clashes with serializer and is invitation to trouble. You can use method like Awake() or Start() to initialize.

What you’re looking for is something called properties. Properties are similar to member variable fields. they use special get and set keywords.
Properties are great and promote encapsulation.

For a high level brief intro to properties see :
https://unity3d.com/learn/tutorials/topics/scripting/properties?playlist=17117

Note-Properties don’t by default show in the inspector.

Additionally, I see that your class creature inherits from stats. This means that your member variables agility, health, speed. Live and are accessable in your creature class even though not visible.

Eg. If your creature class had no implementation or member variables you could still access its (Stats) member variables like this in another script.

Creature myCreature = gameObject.GetComponent<Creature>();
myCreature.agility = 25;

Remember when using polymorphism you are saying, "Creature, IS a Stat, but Stat IS NOT a creature.