I am trying to add a “NPCNeed” component to an NPC gameobject.
Because I cannot use constructors, I have an Init method on my base Need class
abstract Need : Monobehaviour
NPCNeed : Need
I have an NPCNeedFactory class responsible for creating and attaching all the different Needs to the NPC.gameObject that calls it.
However when I state:
//Attaches a new Empty NPCNeed to character (passed through in method sig)
NPCNeed charNeed = character.gameObject.AddComponent<NPCNeed>();
//This does not throw an exception
Debug.Log(charNeed);
//Init fills out the variables for NPCNeed
charNeed.Init( //NullReference throw here: Object reference not set to an instance of an object
j["NPCNeed"]*["needName"].Value,*
NeedStatusFactory(j),
j[“NPCNeed”][“startingValue”].AsInt,
j[“NPCNeed”][“valueDecrementRate”].AsInt,
j[“NPCNeed”][“timeToDecrement”].AsInt //Code doesn’t get here, these just pull vars from a JSON script
);
I can post more code if needed, but I want to believe that I’m missing something obvious. Why hasn’t charNeed become the object reference Init() is looking for?