I’m beginning to create a game on Unity Engine. I have a background of game development using Ash Framework on actionscript, which is as well a component based framework. In comparison with Unity, Ash supports a rather useful feature which allows to perform an update cycle only when the entity (i.e. gameObject) confroms to the specified list of present components.
Let me be more specific. For example I have a component which needs to update some internal parameter based on object visibility and on object position. The code that I come up with right now is some stuff like (in pseudo code)
void Update() {
VisibilityComponent vs = gameObject.get(VisibilityComponent);
PositionComponent ps = gameObject.get(VisibilityComponent);
if (vs != null && ps != null) {
parameter = calculate(vs, ps);
}
}
I think that the above code should execute only when the required components are added to gameObject. Is there any neat way of doing this on Unity? (Like in Ash)
I’m interested if it is possible to perform such filtering in Unity. Or how could I avoid null pointer exceptions during an update cycle of a component in which I have to work with some other components.
The annotation [Require component] is something that I think could be handy, but it has a bit different work pattern. “Require component” creates components, but on the contrary I need to begin work (Update, Start, etc.) only when the specified components exist.