- Home /
Is there a problem with using Monobehaviours for individual Stats?
I am wondering if there are any serious concerns with creating all my state as light-weight monobehaviours as such
class State : Monobehaviour { }
class Health : State
class Armor : State
class Energy : State
class Strength : State
etc
What I want to accomplish is enabling, on literally any entity, the capable of having any possible state.
Examples being characters having an "Emotions" monobehaviour, but a sword or apple could equally have said state.
Any issue you see arrising, especially as a project grows?
Answer by UnityedWeStand · Jul 07, 2020 at 05:36 AM
One issue that may arise is the fact that every MonoBehaviour script that is instantiated will have its Awake/Start functions called on startup and Update() called on every frame at least. I'm not sure how MonoBehaviour scripts without a declared Awake/Start/Update will affect performance, but it could potentially be a problem if you have thousands of units with a dozen stats each.
In any case, there really isn't any reason to make each stat its own extension of MonoBehavior. In fact, there isn't even a good reason to extend the MonoBehavior class at all if all you are doing with stats is storing and retrieving data.
For example, in one of your "base unit" classes, you could declare a public enum of stat types and use them to set an array of stats for each unit. (the code in the Start() function is just an example of how to set the stats)
public class BaseUnit : MonoBehaviour
{
public enum STAT_TYPES
{
HP,
ATTACK,
DEFENSE
}
public float[] stats = new float[3]
void Start()
{
stats[STAT_TYPES.HP] = 10;
stats[STAT_TYPES.ATTACK] = 2;
stats[STAT_TYPES.DEFENSE] = 3;
}
}
Your answer
Follow this Question
Related Questions
Something is missing in my code can any body help me? 0 Answers
how to planning n designing game structure/architecture? 0 Answers
How do I enter/exit structures? 1 Answer
How to structure a multiplayer project 0 Answers
Is it possible to pass references to structures containing array of references as from C# to C dll? 0 Answers