I made a small script for Unity3D, meant to be use as GameComponent for GameObjects. It is supposed to represent values that have a max value, and an actual value. In games that would be the case, for instance, for the Health Points, the Stamina, Magic Points and so on. I would love to hear your feedback.///////////////// This is meant for things like Stamina, or HP. You have a max// value, and an actual value.//// It inherits from MonoBehaviour because it is meant to be used// as a Game Object Component///////////////using UnityEngine;using UnityEngine.UI;public class Range : MonoBehaviour {    private int val, max;    public Slider slider;    // Is/Has/Can    public bool IsDepleted() { return val == 0;    }    public bool IsFull()     { return val == max; }    public bool HasAtLeast(int _val) { return this.val >= _val; }    // Actions    public void Fill()    { val = max; }    public void Deplete() { val = 0;   }    public void Increase(int _val) { this.SetValue(this.val + _val); }    public void Decrease(int _val) { this.SetValue(this.val - _val); }    // Getters    public int GetValue() { return this.val; }    public int GetMax()   { return this.max; }    // Setters    public void SetValue(int amount) {        if (amount > this.max) this.Fill();        else if (amount < 0) this.Deplete();        else this.val = amount;        UpdateSlider();    }    public void SetMax(int amount) {        this.max = amount;        if (this.val > this.max) this.Fill();        UpdateSlider();    }    private void UpdateSlider() {        if (slider) {            slider.maxValue = this.max;            slider.value = val;        }    }}
Range class to represent limited values, like HP, Stamina and so on
c#;unity3d;role playing game
ProblematicFill and Deplete are public, but don't call UpdateSlider, whereas Increment/Decrement do.SetMax should reject values < 0 (0 could be allowed, for value forced to 0)Design StyleThere is a function in the Unity Mathf static class that does bounds checking Mathf.ClampC# has properties, while you can have the pair GetX/SetX they are preferred.public int Value {     get { return val; }     set { val = Mathf.Clamp( value, 0, Max );         UpdateSlider(); } }public int Max {     get { return max; }     set { if ( value < 0 ) throw new ArgumentOutOfRangeException (Max);         max = value; } }ConsiderIncrement and Decrement could be replaced by overriding + and -, as += and -= will use such overloads.