- Home /
Prefab with component, and attached sub component
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.
Answer by supernat · May 21, 2014 at 04:41 AM
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.
Is there a way for the Stat class to detect which Behavior class is attached then assign to "instance"? Or is it possible to have multiple files have the same class?
--Edit-- I guess I'll just add a enum in the Stat class, then add if statements in the Behavior class. Thanks for your help supernat.
Okay, I guess I misunderstood. I thought you meant all of your behaviors were in the same class. Yes, you can check one of two ways. 1) using the "is" keyword or 2) checking for null after each test.
For instance, if you have a BaseBehaviorClass and all specialized classes derive from that, you can do something like this:
BaseBehavior instance = GetComponent<BaseBehavior>();
if (instance is SpecializedBehaviorSnuSnu)
// Do whatever
else if (instance is SpecializedBehaviorAge)
// Do whatever else
In this case, it assumes you have only one behavior attached to your object still. If you had multiple behaviors, it would be more like this (though I may have some code compile issues here, it's the right idea):
BaseBehavior[] behaviors = GetComponentsInChildren<BaseBehavior>();
foreach (BaseBehavior behavior in behaviors) {
if (behavior is SpecializedBehaviorSnuSnu)
// Do whatever
else if (behavior is SpecializedBehaviorAge)
// Do whatever else
}
The alternate approach is to check each behavior for null:
SpecializedBehaviorSnuSnu instance = GetComponent<SpecializedBehaviorSnuSnu>();
if (instance != null) {
// Do whatever
}
// repeat for other types
Note: I don't do the check for null option above very often, so I can't remember, but C# may throw an exception when you say GetComponent() if that component doesn't exist. In that case, just modify the test by placing a try/catch, and if you hit the catch block, you know the component doesn't exist.
Your answer
Follow this Question
Related Questions
Accessing component of a single prefabs' instance. 0 Answers
Spawning a prefab at another object's location 3 Answers
Issue Instantiating prefab in C# 0 Answers
How do I make a clone of a prefab appear on the correct layer? [5.2.2f1] 1 Answer
Prefabs instantiated from an array are keeping their public int value 1 Answer