- Home /
Using components as interfaces?
Before I knew about interfaces, I have been using components to essentially do the same thing. For example, I would have a health component, that would take care of any damage or healing. Rather than having classes implement IDamageble or IHealable, I would simply check to see if a gameobject had a Health script attached.
Can people with experience with Unity tell me upsides or downsides of either? Should I switch to using interfaces instead?
Interfaces are a general program$$anonymous$$g concept, whereas components / $$anonymous$$onoBehaviours are a Unity-specific construct. Without some specific context they aren't really comparable. Either way, if you are avoiding using components in Unity entirely you are probably making things harder for yourself. Having said that, using interfaces alongside components, and perhaps even to reduce the number of components, could potentially make your code more flexible. But you haven't provided enough information for me to go on. If you could provide a more specific example then I could comment further.
Sometimes checking for a script is a good Generic way to implement behaviours. For example, I have used this concept to decide if a Horse is $$anonymous$$ountable and if so, take control of it's Animator. This method allows you to throw in numerous new objects and drop a generic script on to them without necessarily adding code on the player for each one and provides a catch for NullRefs, effectively eli$$anonymous$$ating them. So the method is valid, but in a whole project I may use it only once. Of course, I'm using uJS which is far less useful than C#.
Answer by laian · Aug 15, 2014 at 07:56 PM
I think I got the answer I wanted from the discussion. Thanks guys, seems like there is no hard convention when using an interface or component are both doable. :)
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
UnityEngine.Input.GetMouseButton(1)) issue 1 Answer
I made a better shader how do i fix[add _Shadow Strength]help???>Sorry that im asking for to much 1 Answer
Help In Making a SphereCast for 3D Tire! Working RayCast Script included! 0 Answers
How would I do something. 2 Answers