How to create instances of the base character class
For a class to be bound to GameObject
, it must inherit from MonoBehaviour
. If I create a base character class containing all the attributes shared by both NPCs and PCs, how do I instantiate this class and attach it to game objects? To give a concrete example of the problem, if the base character class has variables like health, stamina, strength, etc. and I want a particular game object to have a certain set of these attributes, how can I bind it to a game object like it can't inherit from base character class?
I suspect the mistake I am making is thinking that these instances should be bound to the objects I want them to be bound to, but some clear guidance here would be most valuable.
source to share
It seems what you really want is a base class that also allows its children to be MonoBehaviours. You can do this by making your base class abstract MonoBehaviour and inheriting from it.
public abstract class Base : MonoBehaviour
{
protected int HP;
}
Then your children of this class will also be MonoBehaviours, which you can attach to GameObjects.
public class Ninja : Base
{
void Start()
{
HP = 100;
}
}
source to share