My goal is to instantiate a prefab that contains 2 components that manage stats and behavior. The stat component will not change, but the behavior component will. Right now my behavior components contain a bunch of methods like onDrop, onAge, onDeathBySnuSnu (you get the point). And yes they are duplicate functions in every behavior component.
Is there a way for the stat component to only see behavior component within the prefab? I’m pretty new with c# so any guidance on the best practice would be much appreciated.
+1 for onDeathBySnuSnu!
To answer your question (if I understand it), yes, you can access the Behavior class instances on the prefab’d object (I mean the clone of the prefab in the game world, so if that’s not what you are asking, sorry). It would be something like this:
// In the Stat class, let's say in the Update method just for example:
void Update() {
MyBehaviorClass instance = GetComponent<MyBehaviorClass>();
instance.onDeathBySnuSnu();
}
As long as the MyBehaviorClass component is attached at the same level in the hierarchy on the game object as the Stat class component, it should be able to retrieve it using GetComponent. If it is higher up or lower down, you can instead say “gameObject.GetComponentInChildren()” I’m assuming there is only one MyBehaviorClass attached to each game object, but there are methods to return arrays of them if needed.